ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PageMetricsService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ilException;
32
33final readonly class PageMetricsService implements ilContentPageObjectConstants
34{
35 public function __construct(
36 private PageMetricsRepository $pageMetricsRepository,
37 private Factory $refinery
38 ) {
39 }
40
41 protected function doesPageExistsForLanguage(int $contentPageId, string $language): bool
42 {
43 return ilContentPagePage::_exists(self::OBJ_TYPE, $contentPageId, $language, true);
44 }
45
46 protected function ensurePageObjectExists(int $contentPageId, string $language): void
47 {
48 if (!$this->doesPageExistsForLanguage($contentPageId, $language)) {
49 $pageObject = new ilContentPagePage();
50 $pageObject->setParentId($contentPageId);
51 $pageObject->setId($contentPageId);
52 $pageObject->setLanguage($language);
53 $pageObject->createFromXML();
54 }
55 }
56
60 public function store(StorePageMetricsCommand $command): void
61 {
62 $this->ensurePageObjectExists($command->getContentPageId(), $command->getLanguage());
63
64 $pageObjectGUI = new ilContentPagePageGUI($command->getContentPageId(), 0, false, $command->getLanguage());
65 $pageObjectGUI->setEnabledTabs(false);
66 $pageObjectGUI->setFileDownloadLink(ILIAS_HTTP_PATH);
67 $pageObjectGUI->setFullscreenLink(ILIAS_HTTP_PATH);
68 $pageObjectGUI->setSourcecodeDownloadScript(ILIAS_HTTP_PATH);
69 $pageObjectGUI->setProfileBackUrl(ILIAS_HTTP_PATH);
70 $text = $pageObjectGUI->getHTML();
71
72 $readingTimeTransformation = $this->refinery->string()->estimatedReadingTime();
73 $readingTime = new PageReadingTime($readingTimeTransformation->transform($text));
74
75 $pageMetrics = new PageMetrics(
76 $command->getContentPageId(),
77 $command->getContentPageId(),
78 $command->getLanguage(),
79 $readingTime
80 );
81 $this->pageMetricsRepository->store($pageMetrics);
82 }
83
87 public function get(GetPageMetricsCommand $command): PageMetrics
88 {
89 return $this->pageMetricsRepository->findBy(
90 $command->getContentPageId(),
91 $command->getContentPageId(),
92 $command->getLanguage()
93 );
94 }
95}
ensurePageObjectExists(int $contentPageId, string $language)
__construct(private PageMetricsRepository $pageMetricsRepository, private Factory $refinery)
doesPageExistsForLanguage(int $contentPageId, string $language)
Builds data types.
Definition: Factory.php:36
@ilCtrl_Calls ilContentPagePageGUI: ilPageEditorGUI, ilEditClipboardGUI, ilMDEditorGUI @ilCtrl_Calls ...
Base class for ILIAS Exception handling.
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
Interface ilContentPageObjectConstants.