Lines 100.00% 59 / 59
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
 create 100.00% 14 / 14 100.00% 1 / 1 1
 get 100.00% 21 / 21 100.00% 1 / 1 6
 update 100.00% 13 / 13 100.00% 1 / 1 1
 delete 100.00% 10 / 10 100.00% 1 / 1 1
31class SeoRepository implements SeoRepositoryInterface
32{
33    public function __construct(
34        private readonly Configuration $configuration,
35    ) {
36    }
37
38    /**
39     * @return bool True if the SEO entity was created
40     */
41    public function create(SeoEntity $seoEntity): bool
42    {
43        $db = $this->configuration->getDb();
44        $seoType = $db->escape($seoEntity->getSeoType()->value);
45
46        $query = sprintf(
47            'INSERT INTO %sfaqseo (id, type, reference_id, reference_language, title, description) '
48            . "VALUES (%d, '%s', %d, '%s', '%s', '%s')",
49            Database::getTablePrefix(),
50            $db->nextId(Database::getTablePrefix() . 'faqseo', 'id'),
51            $seoType,
52            $seoEntity->getReferenceId(),
53            $db->escape($seoEntity->getReferenceLanguage()),
54            $db->escape($seoEntity->getTitle() ?? ''),
55            $db->escape($seoEntity->getDescription() ?? ''),
56        );
57
58        return (bool) $db->query($query);
59    }
60
61    /**
62     * @throws Exception
63     */
64    public function get(SeoEntity $seoEntity): SeoEntity
65    {
66        $db = $this->configuration->getDb();
67        $seoType = $db->escape($seoEntity->getSeoType()->value);
68
69        $query = sprintf(
70            "SELECT * FROM %sfaqseo WHERE type = '%s' AND reference_id = %d AND reference_language = '%s'",
71            Database::getTablePrefix(),
72            $seoType,
73            $seoEntity->getReferenceId(),
74            $db->escape($seoEntity->getReferenceLanguage()),
75        );
76
77        $result = $db->query($query);
78
79        if ($db->numRows($result) > 0) {
80            while (true) {
81                $row = $db->fetchObject($result);
82                if ($row === false || $row === null || $row === []) {
83                    break;
84                }
85
86                $seoEntity
87                    ->setId((int) $row->id)
88                    ->setTitle((string) $row->title)
89                    ->setDescription((string) $row->description)
90                    ->setCreated(new DateTime((string) $row->created));
91            }
92        }
93
94        return $seoEntity;
95    }
96
97    /**
98     * @return bool True if the SEO entity was updated
99     */
100    public function update(SeoEntity $seoEntity): bool
101    {
102        $db = $this->configuration->getDb();
103        $seoType = $db->escape($seoEntity->getSeoType()->value);
104
105        $query = sprintf(
106            "UPDATE %sfaqseo SET title = '%s', description = '%s' "
107            . "WHERE type = '%s' AND reference_id = %d AND reference_language = '%s'",
108            Database::getTablePrefix(),
109            $db->escape($seoEntity->getTitle() ?? ''),
110            $db->escape($seoEntity->getDescription() ?? ''),
111            $seoType,
112            $seoEntity->getReferenceId(),
113            $db->escape($seoEntity->getReferenceLanguage()),
114        );
115
116        return (bool) $db->query($query);
117    }
118
119    /**
120     * @return bool True if the SEO entity was deleted
121     */
122    public function delete(SeoEntity $seoEntity): bool
123    {
124        $db = $this->configuration->getDb();
125        $seoType = $db->escape($seoEntity->getSeoType()->value);
126
127        $query = sprintf(
128            "DELETE FROM %sfaqseo WHERE type = '%s' AND reference_id = %d AND reference_language = '%s'",
129            Database::getTablePrefix(),
130            $seoType,
131            $seoEntity->getReferenceId(),
132            $db->escape($seoEntity->getReferenceLanguage()),
133        );
134
135        return (bool) $db->query($query);
136    }
137}