ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
PageMetricsService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use ilException;
32 
38 {
39  public function __construct(
40  private readonly PageMetricsRepository $pageMetricsRepository,
41  private readonly Factory $refinery
42  ) {
43  }
44 
45  protected function doesPageExistsForLanguage(int $contentPageId, string $language): bool
46  {
47  return ilContentPagePage::_exists(self::OBJ_TYPE, $contentPageId, $language, true);
48  }
49 
50  protected function ensurePageObjectExists(int $contentPageId, string $language): void
51  {
52  if (!$this->doesPageExistsForLanguage($contentPageId, $language)) {
53  $pageObject = new ilContentPagePage();
54  $pageObject->setParentId($contentPageId);
55  $pageObject->setId($contentPageId);
56  $pageObject->setLanguage($language);
57  $pageObject->createFromXML();
58  }
59  }
60 
64  public function store(StorePageMetricsCommand $command): void
65  {
66  $this->ensurePageObjectExists($command->getContentPageId(), $command->getLanguage());
67 
68  $pageObjectGUI = new ilContentPagePageGUI($command->getContentPageId(), 0, false, $command->getLanguage());
69  $pageObjectGUI->setEnabledTabs(false);
70  $pageObjectGUI->setFileDownloadLink(ILIAS_HTTP_PATH);
71  $pageObjectGUI->setFullscreenLink(ILIAS_HTTP_PATH);
72  $pageObjectGUI->setSourcecodeDownloadScript(ILIAS_HTTP_PATH);
73  $pageObjectGUI->setProfileBackUrl(ILIAS_HTTP_PATH);
74  $text = $pageObjectGUI->getHTML();
75 
76  $readingTimeTransformation = $this->refinery->string()->estimatedReadingTime();
77  $readingTime = new PageReadingTime($readingTimeTransformation->transform($text));
78 
79  $pageMetrics = new PageMetrics(
80  $command->getContentPageId(),
81  $command->getContentPageId(),
82  $command->getLanguage(),
83  $readingTime
84  );
85  $this->pageMetricsRepository->store($pageMetrics);
86  }
87 
91  public function get(GetPageMetricsCommand $command): PageMetrics
92  {
93  return $this->pageMetricsRepository->findBy(
94  $command->getContentPageId(),
95  $command->getContentPageId(),
96  $command->getLanguage()
97  );
98  }
99 }
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
doesPageExistsForLanguage(int $contentPageId, string $language)
__construct(private readonly PageMetricsRepository $pageMetricsRepository, private readonly Factory $refinery)
ensurePageObjectExists(int $contentPageId, string $language)
Refinery Factory $refinery