Lines
90.47%
19 / 21
Methods
85.71%
6 / 7
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| logViews | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | ||
| add | 66.66% | 4 / 6 | 0.00% | 0 / 1 | 2.15 | ||
| update | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| getAllData | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| resetAll | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| getRequestTimestamp | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| 31 | readonly class Visits | |
| 32 | { | |
| 33 | private VisitsRepositoryInterface $visitsRepository; | |
| 34 | ||
| 35 | /** | |
| 36 | * Constructor. | |
| 37 | */ | |
| 38 | public function __construct( | |
| 39 | private Configuration $configuration, | |
| 40 | ) { | |
| 41 | $this->visitsRepository = new VisitsRepository($configuration); | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Counting the views of a FAQ record. | |
| 46 | * | |
| 47 | * @param int $faqId FAQ record ID | |
| 48 | */ | |
| 49 | public function logViews(int $faqId): void | |
| 50 | { | |
| 51 | $language = $this->configuration->getLanguage()->getLanguage(); | |
| 52 | $visitCount = $this->visitsRepository->getVisitCount($faqId, $language); | |
| 53 | ||
| 54 | if ($visitCount === 0) { | |
| 55 | $this->add($faqId); | |
| 56 | return; | |
| 57 | } | |
| 58 | ||
| 59 | $this->update($faqId); | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Adds a new entry in the table "faqvisits". | |
| 64 | * | |
| 65 | * @param int $faqId Record ID | |
| 66 | */ | |
| 67 | public function add(int $faqId): bool | |
| 68 | { | |
| 69 | $language = $this->configuration->getLanguage()->getLanguage(); | |
| 70 | $timestamp = $this->getRequestTimestamp(); | |
| 71 | ||
| 72 | // If a row already exists for this (id, lang), update it instead of inserting to avoid unique constraint errors | |
| 73 | if ($this->visitsRepository->exists($faqId, $language)) { | |
| 74 | $this->update($faqId); | |
| 75 | return true; | |
| 76 | } | |
| 77 | ||
| 78 | return $this->visitsRepository->insert($faqId, $language, $timestamp); | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Updates an entry in the table "faqvisits". | |
| 83 | * | |
| 84 | * @param int $faqId FAQ record ID | |
| 85 | */ | |
| 86 | private function update(int $faqId): void | |
| 87 | { | |
| 88 | $language = $this->configuration->getLanguage()->getLanguage(); | |
| 89 | $timestamp = $this->getRequestTimestamp(); | |
| 90 | ||
| 91 | $this->visitsRepository->update($faqId, $language, $timestamp); | |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Get all the entries from the table "faqvisits". | |
| 96 | * | |
| 97 | * @return array<int, array<string, mixed>> | |
| 98 | */ | |
| 99 | public function getAllData(): array | |
| 100 | { | |
| 101 | return $this->visitsRepository->getAll(); | |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Resets all visits to the current date and one visit per FAQ. | |
| 106 | */ | |
| 107 | public function resetAll(): bool | |
| 108 | { | |
| 109 | $timestamp = $this->getRequestTimestamp(); | |
| 110 | return $this->visitsRepository->resetAll($timestamp); | |
| 111 | } | |
| 112 | ||
| 113 | private function getRequestTimestamp(): int | |
| 114 | { | |
| 115 | $timestamp = Request::createFromGlobals()->server->get(key: 'REQUEST_TIME'); | |
| 116 | ||
| 117 | return is_int($timestamp) ? $timestamp : time(); | |
| 118 | } | |
| 119 | } |