Lines
97.14%
34 / 35
Methods
85.71%
6 / 7
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| isFaqBookmark | 90.00% | 9 / 10 | 0.00% | 0 / 1 | 6.04 | ||
| add | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| getAll | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| remove | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| removeAll | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| getBookmarkList | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | ||
| 29 | class Bookmark | |
| 30 | { | |
| 31 | /** | |
| 32 | * @var array<int, \stdClass>|null | |
| 33 | */ | |
| 34 | private ?array $bookmarkCache = null; | |
| 35 | ||
| 36 | private readonly BookmarkRepository $bookmarkRepository; | |
| 37 | ||
| 38 | private readonly BookmarkFormatter $bookmarkFormatter; | |
| 39 | ||
| 40 | /** | |
| 41 | * Constructor. | |
| 42 | * | |
| 43 | * @param Configuration $configuration Configuration object | |
| 44 | * @param CurrentUser $currentUser CurrentUser object | |
| 45 | */ | |
| 46 | public function __construct( | |
| 47 | private readonly Configuration $configuration, | |
| 48 | private readonly CurrentUser $currentUser, | |
| 49 | ) { | |
| 50 | $this->bookmarkRepository = new BookmarkRepository($this->configuration, $this->currentUser); | |
| 51 | $this->bookmarkFormatter = new BookmarkFormatter($this->configuration, $this->currentUser); | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Returns true if a given FAQ ID is a bookmark of the current User. | |
| 56 | * Returns false if not. | |
| 57 | * | |
| 58 | * @param int $faqId ID of the Faq | |
| 59 | */ | |
| 60 | public function isFaqBookmark(int $faqId): bool | |
| 61 | { | |
| 62 | if ($faqId <= 0) { | |
| 63 | return false; | |
| 64 | } | |
| 65 | ||
| 66 | $bookmarks = $this->getAll(); | |
| 67 | if ($bookmarks === []) { | |
| 68 | return false; | |
| 69 | } | |
| 70 | ||
| 71 | foreach ($bookmarks as $bookmark) { | |
| 72 | if (!(property_exists($bookmark, 'faqid') && (int) $bookmark->faqid === $faqId)) { | |
| 73 | continue; | |
| 74 | } | |
| 75 | ||
| 76 | return true; | |
| 77 | } | |
| 78 | ||
| 79 | return false; | |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Saves a given FAQ to the bookmark collection of the current user. | |
| 84 | * | |
| 85 | * @param int $faqId ID of the Faq | |
| 86 | */ | |
| 87 | public function add(int $faqId): bool | |
| 88 | { | |
| 89 | $result = $this->bookmarkRepository->add($faqId); | |
| 90 | ||
| 91 | if ($result) { | |
| 92 | $this->bookmarkCache = null; | |
| 93 | } | |
| 94 | ||
| 95 | return $result; | |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Gets all bookmarks from the current user. | |
| 100 | * | |
| 101 | * @return array<int, \stdClass> List of DB result objects each containing ->faqid | |
| 102 | */ | |
| 103 | public function getAll(): array | |
| 104 | { | |
| 105 | if ($this->bookmarkCache !== null) { | |
| 106 | return $this->bookmarkCache; | |
| 107 | } | |
| 108 | ||
| 109 | $this->bookmarkCache = $this->bookmarkRepository->getAll(); | |
| 110 | ||
| 111 | return $this->bookmarkCache; | |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Removes a bookmark from the current user. | |
| 116 | * | |
| 117 | * @param int $faqId ID of the Faq | |
| 118 | */ | |
| 119 | public function remove(int $faqId): bool | |
| 120 | { | |
| 121 | $result = $this->bookmarkRepository->remove($faqId); | |
| 122 | ||
| 123 | if ($result) { | |
| 124 | $this->bookmarkCache = null; | |
| 125 | } | |
| 126 | ||
| 127 | return $result; | |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Removes all bookmarks from the current user. | |
| 132 | */ | |
| 133 | public function removeAll(): bool | |
| 134 | { | |
| 135 | $result = $this->bookmarkRepository->removeAll(); | |
| 136 | ||
| 137 | if ($result) { | |
| 138 | $this->bookmarkCache = null; | |
| 139 | } | |
| 140 | ||
| 141 | return $result; | |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * @return array<int, array{url:string,title:string,id:int,answer:string}> | |
| 146 | */ | |
| 147 | public function getBookmarkList(): array | |
| 148 | { | |
| 149 | $bookmarks = $this->getAll(); | |
| 150 | $list = []; | |
| 151 | ||
| 152 | foreach ($bookmarks as $bookmark) { | |
| 153 | $item = $this->bookmarkFormatter->format($bookmark); | |
| 154 | if ($item !== null) { | |
| 155 | $list[] = $item; | |
| 156 | } | |
| 157 | } | |
| 158 | ||
| 159 | return $list; | |
| 160 | } | |
| 161 | } |