Lines 100.00% 31 / 31
Methods 100.00% 5 / 5
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 add 100.00% 8 / 8 100.00% 1 / 1 2
 getAll 100.00% 8 / 8 100.00% 1 / 1 2
 remove 100.00% 8 / 8 100.00% 1 / 1 2
 removeAll 100.00% 6 / 6 100.00% 1 / 1 1
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}