Lines 100.00% 38 / 38
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
 setUser 100.00% 2 / 2 100.00% 1 / 1 1
 setGroups 100.00% 2 / 2 100.00% 1 / 1 1
 setLanguage 100.00% 2 / 2 100.00% 1 / 1 1
 getCategories 100.00% 31 / 31 100.00% 1 / 1 7
27class Startpage
28{
29    private int $user = -1;
30
31    /** @var int[] */
32    private array $groups = [-1];
33
34    private string $language = '';
35
36    public function __construct(
37        private readonly Configuration $configuration,
38    ) {
39    }
40
41    public function setUser(int $user): Startpage
42    {
43        $this->user = $user;
44        return $this;
45    }
46
47    /**
48     * @param int[] $groups
49     */
50    public function setGroups(array $groups): Startpage
51    {
52        $this->groups = $groups;
53        return $this;
54    }
55
56    public function setLanguage(string $language): Startpage
57    {
58        $this->language = $language;
59        return $this;
60    }
61
62    /**
63     * Gets all categories and write them in an array.
64     *
65     * @return array<int, array<string, mixed>>
66     */
67    public function getCategories(): array
68    {
69        $categories = [];
70        $where = '';
71
72        if (preg_match("/^[a-z\-]{2,}$/", $this->language)) {
73            $where = "AND fc.lang = '" . $this->configuration->getDb()->escape($this->language) . "'";
74        }
75
76        $query = sprintf(
77            '
78            SELECT
79                fc.id AS id,
80                fc.lang AS lang,
81                fc.parent_id AS parent_id,
82                fc.name AS name,
83                fc.description AS description,
84                fc.user_id AS user_id,
85                fc.group_id AS group_id,
86                fc.active AS active,
87                fc.image AS image,
88                fc.show_home AS show_home,
89                fco.position AS position
90            FROM
91                %sfaqcategories fc
92            LEFT JOIN
93                %sfaqcategory_group fg
94            ON
95                fc.id = fg.category_id
96            LEFT JOIN
97                %sfaqcategory_order fco
98            ON
99                fc.id = fco.category_id
100            LEFT JOIN
101                %sfaqcategory_user fu
102            ON
103                fc.id = fu.category_id
104            WHERE 
105                ( fg.group_id IN (%s)
106            OR
107                (fu.user_id = %d AND fg.group_id IN (%s)))
108            AND
109                fc.active = 1
110            AND
111                fc.show_home = 1
112                %s
113            GROUP BY
114                fc.id, fc.lang, fc.name, fc.description, fc.user_id, fc.group_id, fc.active, fc.image, 
115                fc.show_home, fco.position
116            ORDER BY
117                fco.position, fc.id ASC
118
119                ',
120            Database::getTablePrefix(),
121            Database::getTablePrefix(),
122            Database::getTablePrefix(),
123            Database::getTablePrefix(),
124            implode(', ', $this->groups),
125            $this->user,
126            implode(', ', $this->groups),
127            $where,
128        );
129
130        $result = $this->configuration->getDb()->query($query);
131
132        while (true) {
133            $row = $this->configuration->getDb()->fetchArray($result);
134            if ($row === false || $row === null || $row === []) {
135                break;
136            }
137
138            $link = sprintf('./category/%d/%s.html', (int) $row['id'], TitleSlugifier::slug((string) $row['name']));
139            $image = '' === $row['image'] ? '' : 'content/user/images/' . (string) $row['image'];
140
141            $category = [
142                'url' => $link,
143                'name' => $row['name'],
144                'description' => $row['description'],
145                'image' => $image,
146            ];
147
148            $categories[] = $category;
149        }
150
151        return $categories;
152    }
153}