Lines 100.00% 38 / 38
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 create 100.00% 10 / 10 100.00% 1 / 1 1
 get 100.00% 20 / 20 100.00% 1 / 1 8
 delete 100.00% 7 / 7 100.00% 1 / 1 1
30readonly class Revision
31{
32    /**
33     * Revision constructor.
34     */
35    public function __construct(
36        private Configuration $configuration,
37    ) {
38    }
39
40    /**
41     * Adds a new revision from a given FAQ ID and FAQ language
42     */
43    public function create(int $faqId, string $faqLanguage): bool
44    {
45        $query = sprintf(
46            "
47            INSERT INTO 
48                %sfaqdata_revisions 
49            SELECT 
50                id, lang, solution_id, revision_id + 1, active, sticky, keywords, thema, content, author, email, 
51                comment, updated, date_start, date_end, created, notes, sticky_order 
52            FROM 
53                %sfaqdata 
54            WHERE 
55                id = %d 
56              AND 
57                lang = '%s'",
58            Database::getTablePrefix(),
59            Database::getTablePrefix(),
60            $faqId,
61            $this->configuration->getDb()->escape($faqLanguage),
62        );
63
64        $this->configuration->getDb()->query($query);
65
66        return true;
67    }
68
69    /**
70     * Gets all revisions from a given FAQ ID and FAQ language
71     *
72     * @return list<array{revision_id: int, updated: string, author: string}>
73     */
74    public function get(int $faqId, string $faqLanguage, string $faqAuthor): array
75    {
76        $revisionData = [];
77
78        $query = sprintf(
79            "SELECT 
80                revision_id, updated, author FROM %sfaqdata_revisions
81            WHERE
82                id = %d
83            AND
84                lang = '%s'
85            ORDER BY 
86                revision_id",
87            Database::getTablePrefix(),
88            $faqId,
89            $this->configuration->getDb()->escape($faqLanguage),
90        );
91
92        $result = $this->configuration->getDb()->query($query);
93
94        if ($this->configuration->getDb()->numRows($result) > 0) {
95            while (true) {
96                $row = $this->configuration->getDb()->fetchObject($result);
97                if ($row === false || $row === null || $row === []) {
98                    break;
99                }
100
101                $revisionData[] = [
102                    'revision_id' => (int) $row->revision_id,
103                    'updated' => $faqId === 0 ? date(format: 'YmdHis') : (string) $row->updated,
104                    'author' => $faqId === 0 ? ucfirst($faqAuthor) : (string) $row->author,
105                ];
106            }
107        }
108
109        return $revisionData;
110    }
111
112    /**
113     * Deletes all revisions for a given FAQ ID and FAQ language
114     */
115    public function delete(int $faqId, string $faqLanguage): bool
116    {
117        $query = sprintf(
118            "DELETE FROM %sfaqdata_revisions WHERE id = %d AND lang = '%s'",
119            Database::getTablePrefix(),
120            $faqId,
121            $this->configuration->getDb()->escape($faqLanguage),
122        );
123
124        return (bool) $this->configuration->getDb()->query($query);
125    }
126}