Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
ConfigurationRepository
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
8 / 8
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
3
 updateConfigValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetchAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 insert
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renameKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFaqDataContents
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 updateFaqDataContentById
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * The configuration 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-03
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Configuration;
21
22use phpMyFAQ\Configuration as CoreConfiguration;
23use phpMyFAQ\Configuration\Storage\ConfigurationStorageSettingsResolver;
24use phpMyFAQ\Configuration\Storage\DatabaseConfigurationStore;
25use phpMyFAQ\Configuration\Storage\FilesystemConfigurationCache;
26use phpMyFAQ\Configuration\Storage\HybridConfigurationStore;
27use phpMyFAQ\Database;
28use phpMyFAQ\Environment;
29use phpMyFAQ\System;
30
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}