Lines 100.00% 42 / 42
Methods 100.00% 8 / 8
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 21 / 21 100.00% 1 / 1 3
 updateConfigValue 100.00% 1 / 1 100.00% 1 / 1 1
 fetchAll 100.00% 1 / 1 100.00% 1 / 1 1
 insert 100.00% 1 / 1 100.00% 1 / 1 1
 delete 100.00% 1 / 1 100.00% 1 / 1 1
 renameKey 100.00% 1 / 1 100.00% 1 / 1 1
 getFaqDataContents 100.00% 6 / 6 100.00% 1 / 1 2
 updateFaqDataContentById 100.00% 10 / 10 100.00% 1 / 1 1
31class ConfigurationRepository
32{
33    private DatabaseConfigurationStore $databaseConfigurationStore;
34
35    private HybridConfigurationStore $hybridConfigurationStore;
36
37    public function __construct(
38        private CoreConfiguration $coreConfiguration,
39        string $tableName = 'faqconfig',
40    ) {
41        $this->databaseConfigurationStore = new DatabaseConfigurationStore(
42            $this->coreConfiguration->getDb(),
43            $tableName,
44        );
45        $settingsResolver = new ConfigurationStorageSettingsResolver($this->databaseConfigurationStore);
46        $this->hybridConfigurationStore = new HybridConfigurationStore(
47            $this->databaseConfigurationStore,
48            $settingsResolver,
49            $this->coreConfiguration->getLogger(),
50            FilesystemConfigurationCache::createIfEnabled(
51                debug: Environment::isDebugMode() || System::isDevelopmentVersion(),
52                enabled: Environment::get('CONFIG_CACHE_ENABLED'),
53                cacheDir: (string) PMF_ROOT_DIR . '/content/core/cache',
54                identity: sprintf(
55                    '%s|%s|%s',
56                    defined('PMF_CONFIG_DIR') ? (string) PMF_CONFIG_DIR : '',
57                    Database::getTablePrefix(),
58                    $tableName,
59                ),
60            ),
61        );
62    }
63
64    public function updateConfigValue(string $key, string $value): bool
65    {
66        return $this->hybridConfigurationStore->updateConfigValue($key, $value);
67    }
68
69    /**
70     * @return array<int, \stdClass>
71     */
72    public function fetchAll(): array
73    {
74        return $this->hybridConfigurationStore->fetchAll();
75    }
76
77    public function insert(string $name, string $value): bool
78    {
79        return $this->hybridConfigurationStore->insert($name, $value);
80    }
81
82    public function delete(string $name): bool
83    {
84        return $this->hybridConfigurationStore->delete($name);
85    }
86
87    public function renameKey(string $currentKey, string $newKey): bool
88    {
89        return $this->hybridConfigurationStore->renameKey($currentKey, $newKey);
90    }
91
92    /**
93     * @return array<int, \stdClass>
94     */
95    public function getFaqDataContents(): array
96    {
97        $sql = <<<'SQL'
98                SELECT id, lang, content FROM %sfaqdata
99            SQL;
100        $query = sprintf($sql, Database::getTablePrefix());
101        $response = $this->coreConfiguration->getDb()->query($query);
102        $rows = $this->coreConfiguration->getDb()->fetchAll($response);
103        return is_array($rows) ? $rows : [];
104    }
105
106    public function updateFaqDataContentById(int $faqId, string $lang, string $newContent): bool
107    {
108        $sql = <<<'SQL'
109                UPDATE %sfaqdata SET content='%s' WHERE id=%d AND lang='%s'
110            SQL;
111        $query = sprintf(
112            $sql,
113            Database::getTablePrefix(),
114            $this->coreConfiguration->getDb()->escape($newContent),
115            $faqId,
116            $this->coreConfiguration->getDb()->escape($lang),
117        );
118        return (bool) $this->coreConfiguration->getDb()->query($query);
119    }
120}