Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
BookmarkRepository
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 getAll
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 remove
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 removeAll
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Bookmark Repository.
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    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @author    Jan Harms <model_railroader@gmx-topmail.de>
13 * @copyright 2025 phpMyFAQ Team
14 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15 * @link      https://www.phpmyfaq.de
16 * @since     2025-10-13
17 */
18
19declare(strict_types=1);
20
21namespace phpMyFAQ\Bookmark;
22
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Database;
25use phpMyFAQ\User\CurrentUser;
26
27readonly class BookmarkRepository implements BookmarkRepositoryInterface
28{
29    public function __construct(
30        private Configuration $configuration,
31        private CurrentUser $currentUser,
32    ) {
33    }
34
35    public function add(int $faqId): bool
36    {
37        if ($faqId <= 0) {
38            return false;
39        }
40
41        $table = Database::getTablePrefix() . 'faqbookmarks';
42        $userId = $this->currentUser->getUserId();
43
44        $query = <<<SQL
45                INSERT INTO {$table} (userid, faqid) VALUES ({$userId},{$faqId})
46            SQL;
47
48        return (bool) $this->configuration->getDb()->query($query);
49    }
50
51    /**
52     * @return array<int, \stdClass>
53     */
54    public function getAll(): array
55    {
56        $table = Database::getTablePrefix() . 'faqbookmarks';
57        $userId = $this->currentUser->getUserId();
58
59        $query = <<<SQL
60                SELECT faqid FROM {$table} WHERE userid = {$userId}
61            SQL;
62
63        $result = $this->configuration->getDb()->query($query);
64        $data = $this->configuration->getDb()->fetchAll($result);
65
66        return is_array($data) ? $data : [];
67    }
68
69    public function remove(int $faqId): bool
70    {
71        if ($faqId <= 0) {
72            return false;
73        }
74
75        $table = Database::getTablePrefix() . 'faqbookmarks';
76        $userId = $this->currentUser->getUserId();
77
78        $query = <<<SQL
79                DELETE FROM {$table} WHERE userid = {$userId} AND faqid = {$faqId}
80            SQL;
81
82        return (bool) $this->configuration->getDb()->query($query);
83    }
84
85    public function removeAll(): bool
86    {
87        $table = Database::getTablePrefix() . 'faqbookmarks';
88        $userId = $this->currentUser->getUserId();
89
90        $query = <<<SQL
91                DELETE FROM {$table} WHERE userid = {$userId}
92            SQL;
93
94        return (bool) $this->configuration->getDb()->query($query);
95    }
96}