Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
59 / 59 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| SeoRepository | |
100.00% |
59 / 59 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
1 / 1 |
| __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 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SEO repository class |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2025-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2025-11-23 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Seo; |
| 21 | |
| 22 | use DateTime; |
| 23 | use Exception; |
| 24 | use phpMyFAQ\Configuration; |
| 25 | use phpMyFAQ\Database; |
| 26 | use phpMyFAQ\Entity\SeoEntity; |
| 27 | |
| 28 | /** |
| 29 | * Repository for persisting and retrieving SEO entities. |
| 30 | */ |
| 31 | class 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 | } |