Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
85.71% covered (success)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bookmark
97.14% covered (success)
97.14%
34 / 35
85.71% covered (success)
85.71%
6 / 7
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isFaqBookmark
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
 add
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 remove
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 removeAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getBookmarkList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Bookmark handling.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Jan Harms <model_railroader@gmx-topmail.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2023-07-19
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ;
21
22use phpMyFAQ\Bookmark\BookmarkFormatter;
23use phpMyFAQ\Bookmark\BookmarkRepository;
24use phpMyFAQ\User\CurrentUser;
25
26/**
27 * Class Bookmark
28 */
29class 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}