Lines 100.00% 72 / 72
Methods 100.00% 12 / 12
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 createMain 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Instance] __construct 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Instance] create 100.00% 15 / 15 100.00% 1 / 1 2
 [phpMyFAQ\Instance] getId 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Instance] setId 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Instance] getAll 100.00% 4 / 4 100.00% 1 / 1 1
 [phpMyFAQ\Instance] getById 100.00% 5 / 5 100.00% 1 / 1 2
 [phpMyFAQ\Instance] update 100.00% 10 / 10 100.00% 1 / 1 1
 [phpMyFAQ\Instance] delete 100.00% 11 / 11 100.00% 1 / 1 3
 [phpMyFAQ\Instance] addConfig 100.00% 9 / 9 100.00% 1 / 1 1
 [phpMyFAQ\Instance] getConfig 100.00% 6 / 6 100.00% 1 / 1 5
 [phpMyFAQ\Instance] getInstanceConfig 100.00% 7 / 7 100.00% 1 / 1 2
29class Main extends Instance
30{
31    public function createMain(Instance $instance): void
32    {
33        $this->setId($instance->getId());
34        $this->addConfig('isMaster', 'true');
35    }
36}

Inherited from phpMyFAQ\Instance

47    public function __construct(
48        protected Configuration $configuration,
49    ) {
50    }
57    public function create(InstanceEntity $instanceEntity): int
58    {
59        $this->setId($this->configuration->getDb()->nextId(Database::getTablePrefix() . 'faqinstances', column: 'id'));
60
61        $query = "INSERT INTO %sfaqinstances VALUES (%d, '%s', '%s', '%s', %s, %s)";
62        $insert = sprintf(
63            $query,
64            Database::getTablePrefix(),
65            $this->getId(),
66            $this->configuration->getDb()->escape($instanceEntity->getUrl()),
67            $this->configuration->getDb()->escape($instanceEntity->getInstance()),
68            $this->configuration->getDb()->escape($instanceEntity->getComment()),
69            $this->configuration->getDb()->now(),
70            $this->configuration->getDb()->now(),
71        );
72
73        if (!$this->configuration->getDb()->query($insert)) {
74            return 0;
75        }
76
77        return $this->getId();
78    }
83    public function getId(): int
84    {
85        return $this->id;
86    }
91    public function setId(int $id): void
92    {
93        $this->id = $id;
94    }
101    public function getAll(): array
102    {
103        $query = 'SELECT * FROM %sfaqinstances ORDER BY id';
104        $select = sprintf($query, Database::getTablePrefix());
105
106        $result = $this->configuration->getDb()->query($select);
107
108        return $this->configuration->getDb()->fetchAll($result) ?? [];
109    }
114    public function getById(int $id): \stdClass
115    {
116        $query = 'SELECT * FROM %sfaqinstances WHERE id = %d';
117        $select = sprintf($query, Database::getTablePrefix(), $id);
118
119        $result = $this->configuration->getDb()->query($select);
120
121        $row = $this->configuration->getDb()->fetchObject($result);
122
123        return $row instanceof \stdClass ? $row : new \stdClass();
124    }
129    public function update(int $id, InstanceEntity $instanceEntity): bool
130    {
131        $query = "UPDATE %sfaqinstances SET instance = '%s', comment = '%s', url = '%s' WHERE id = %d";
132        $update = sprintf(
133            $query,
134            Database::getTablePrefix(),
135            $this->configuration->getDb()->escape($instanceEntity->getInstance()),
136            $this->configuration->getDb()->escape($instanceEntity->getComment()),
137            $this->configuration->getDb()->escape($instanceEntity->getUrl()),
138            $id,
139        );
140
141        return (bool) $this->configuration->getDb()->query($update);
142    }
148    public function delete(int $id): bool
149    {
150        $queryDeleteInstance = 'DELETE FROM %sfaqinstances WHERE id = %d';
151        $queryDeleteConfig = 'DELETE FROM %sfaqinstances_config WHERE instance_id = %d';
152
153        $deletes = [
154            sprintf($queryDeleteInstance, Database::getTablePrefix(), $id),
155            sprintf($queryDeleteConfig, Database::getTablePrefix(), $id),
156        ];
157
158        foreach ($deletes as $delete) {
159            $success = $this->configuration->getDb()->query($delete);
160            if (!$success) {
161                return false;
162            }
163        }
164
165        return true;
166    }
172    public function addConfig(string $name, string $value): mixed
173    {
174        $query = "INSERT INTO %sfaqinstances_config VALUES (%d, '%s', '%s')";
175        $insert = sprintf(
176            $query,
177            Database::getTablePrefix(),
178            $this->getId(),
179            $this->configuration->getDb()->escape(trim($name)),
180            $this->configuration->getDb()->escape(trim($value)),
181        );
182
183        return (bool) $this->configuration->getDb()->query($insert);
184    }
189    public function getConfig(string $name): bool|string
190    {
191        if (!array_key_exists($name, $this->instanceConfig)) {
192            $this->getInstanceConfig($this->getId());
193        }
194
195        return match ($this->instanceConfig[$name]) {
196            'true' => true,
197            'false' => false,
198            default => $this->instanceConfig[$name],
199        };
200    }
207    public function getInstanceConfig(int $instanceId): array
208    {
209        $query = 'SELECT config_name, config_value FROM %sfaqinstances_config WHERE instance_id = %d';
210        $query = sprintf($query, Database::getTablePrefix(), $instanceId);
211
212        $result = $this->configuration->getDb()->query($query);
213        $config = $this->configuration->getDb()->fetchAll($result) ?? [];
214
215        foreach ($config as $items) {
216            $this->instanceConfig[(string) $items->config_name] = (string) $items->config_value;
217        }
218
219        return $this->instanceConfig;
220    }