Lines 100.00% 31 / 31
Methods 100.00% 3 / 3
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% 14 / 14 100.00% 1 / 1 1
 getByFaqId 100.00% 16 / 16 100.00% 1 / 1 4
31readonly class Changelog
32{
33    /**
34     * Changelog constructor.
35     */
36    public function __construct(
37        private Configuration $configuration,
38    ) {
39    }
40
41    /**
42     * Adds a new changelog entry in the table "faqchanges".
43     */
44    public function add(int $id, int $userId, string $text, string $lang, int $revisionId = 0): bool
45    {
46        $db = $this->configuration->getDb();
47        $query = sprintf(
48            "INSERT INTO
49                %sfaqchanges
50            (id, beitrag, lang, revision_id, usr, datum, what)
51                VALUES
52            (%d, %d, '%s', %d, %d, %d, '%s')",
53            Database::getTablePrefix(),
54            $db->nextId(Database::getTablePrefix() . 'faqchanges', 'id'),
55            $id,
56            $db->escape($lang),
57            $revisionId,
58            $userId,
59            (int) Request::createFromGlobals()->server->get('REQUEST_TIME'),
60            $db->escape($text),
61        );
62
63        return (bool) $db->query($query);
64    }
65
66    /**
67     * Returns the changelog of a FAQ record.
68     */
69    public function getByFaqId(int $recordId): array
70    {
71        $entries = [];
72
73        $query = sprintf('
74            SELECT
75                DISTINCT revision_id, usr, datum, what
76            FROM
77                %sfaqchanges
78            WHERE
79                beitrag = %d
80            ORDER BY revision_id, datum DESC', Database::getTablePrefix(), $recordId);
81
82        $result = $this->configuration->getDb()->query($query);
83        if ($result) {
84            while (true) {
85                $row = $this->configuration->getDb()->fetchObject($result);
86                if (!$row) {
87                    break;
88                }
89
90                $entries[] = [
91                    'revision_id' => $row->revision_id,
92                    'user' => $row->usr,
93                    'date' => $row->datum,
94                    'changelog' => $row->what,
95                ];
96            }
97        }
98
99        return $entries;
100    }
101}