ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
PageMetricsService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use ilException;
32 
38 {
40  private Factory $refinery;
41 
42  public function __construct(PageMetricsRepository $pageMetricsRepository, Factory $refinery)
43  {
44  $this->pageMetricsRepository = $pageMetricsRepository;
45  $this->refinery = $refinery;
46  }
47 
48  protected function doesPageExistsForLanguage(int $contentPageId, string $language): bool
49  {
50  return ilContentPagePage::_exists(self::OBJ_TYPE, $contentPageId, $language, true);
51  }
52 
53  protected function ensurePageObjectExists(int $contentPageId, string $language): void
54  {
55  if (!$this->doesPageExistsForLanguage($contentPageId, $language)) {
56  $pageObject = new ilContentPagePage();
57  $pageObject->setParentId($contentPageId);
58  $pageObject->setId($contentPageId);
59  $pageObject->setLanguage($language);
60  $pageObject->createFromXML();
61  }
62  }
63 
68  public function store(StorePageMetricsCommand $command): void
69  {
70  $this->ensurePageObjectExists($command->getContentPageId(), $command->getLanguage());
71 
72  $pageObjectGUI = new ilContentPagePageGUI($command->getContentPageId(), 0, false, $command->getLanguage());
73  $pageObjectGUI->setEnabledTabs(false);
74  $pageObjectGUI->setFileDownloadLink(ILIAS_HTTP_PATH);
75  $pageObjectGUI->setFullscreenLink(ILIAS_HTTP_PATH);
76  $pageObjectGUI->setSourcecodeDownloadScript(ILIAS_HTTP_PATH);
77  $pageObjectGUI->setProfileBackUrl(ILIAS_HTTP_PATH);
78  $text = $pageObjectGUI->getHTML();
79 
80  $readingTimeTransformation = $this->refinery->string()->estimatedReadingTime();
81  $readingTime = new PageReadingTime($readingTimeTransformation->transform($text));
82 
83  $pageMetrics = new PageMetrics(
84  $command->getContentPageId(),
85  $command->getContentPageId(),
86  $command->getLanguage(),
88  );
89  $this->pageMetricsRepository->store($pageMetrics);
90  }
91 
97  public function get(GetPageMetricsCommand $command): PageMetrics
98  {
99  return $this->pageMetricsRepository->findBy(
100  $command->getContentPageId(),
101  $command->getContentPageId(),
102  $command->getLanguage()
103  );
104  }
105 }
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(PageMetricsRepository $pageMetricsRepository, Factory $refinery)
ensurePageObjectExists(int $contentPageId, string $language)