Lines 100.00% 55 / 55
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Config 100.00% 55 / 55 100.00% 7 / 7 27 100.00% 1 / 1
 readEntity 100.00% 10 / 10 100.00% 1 / 1 5
 updateEntity 100.00% 22 / 22 100.00% 1 / 1 9
 readProperty 100.00% 4 / 4 100.00% 1 / 1 2
 replaceProperty 100.00% 6 / 6 100.00% 1 / 1 3
 deleteProperty 100.00% 5 / 5 100.00% 1 / 1 3
 fetchData 100.00% 5 / 5 100.00% 1 / 1 2
 getSpecifiedValue 100.00% 3 / 3 100.00% 1 / 1 3
1<?php
2
3namespace BO\Zmsbackend\Config\Service;
4
5use BO\Zmsbackend\Application as App;
6use BO\Zmsentities\Config as Entity;
7
8class Config extends \BO\Zmsbackend\Base
9{
10    /**
11     *
12     * @return \BO\Zmsentities\Config
13     */
14    public function readEntity(bool $disableCache = false)
15    {
16        $cacheKey = "config";
17
18        if (!$disableCache && App::$cache) {
19            $cached = App::$cache->get($cacheKey);
20            if ($cached instanceof Entity) {
21                return $cached;
22            }
23        }
24
25        $query = \BO\Zmsbackend\Config\Repository\Config::QUERY_SELECT;
26        $config = $this->fetchData($query);
27
28        if (App::$cache) {
29            App::$cache->set($cacheKey, $config);
30        }
31
32        return $config;
33    }
34
35    public function updateEntity(Entity $config): Entity|null
36    {
37        $compareEntity = $this->readEntity(true);
38        $result = false;
39        $query = new \BO\Zmsbackend\Config\Repository\Config(\BO\Zmsbackend\Query\Base::REPLACE);
40        foreach ($config as $key => $item) {
41            if (is_array($item)) {
42                foreach ($item as $itemName => $itemValue) {
43                    if ($itemValue && $compareEntity->getPreference($key, $itemName) != $itemValue) {
44                        $query->addValues(array(
45                            'name' => $key . '__' . $itemName,
46                            'value' => $this->getSpecifiedValue($itemValue),
47                            'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s')
48                        ));
49                        $result = $this->writeItem($query);
50                    }
51                }
52            } else {
53                $query->addValues(array(
54                    'name' => $key,
55                    'value' => $this->getSpecifiedValue($item),
56                    'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s')
57                ));
58                $result = $this->writeItem($query);
59            }
60        }
61
62        if (App::$cache && App::$cache->has('config')) {
63            App::$cache->delete('config');
64        }
65
66        return ($result) ? $this->readEntity(true) : null;
67    }
68
69    public function readProperty(string $property, bool $forUpdate = false)
70    {
71        $sql = \BO\Zmsbackend\Config\Repository\Config::QUERY_SELECT_PROPERTY;
72        if ($forUpdate) {
73            $sql .= " FOR UPDATE";
74        }
75        return $this->fetchValue($sql, [$property]);
76    }
77
78    public function replaceProperty(string $property, string $value)
79    {
80        if (App::$cache && App::$cache->has('config')) {
81            App::$cache->delete('config');
82        }
83
84        return $this->perform(\BO\Zmsbackend\Config\Repository\Config::QUERY_REPLACE_PROPERTY, [
85            'property' => $property,
86            'value' => $value,
87        ]);
88    }
89
90    /**
91     * remove config data
92     *
93     *
94     * @return Entity|null
95     */
96    public function deleteProperty($property)
97    {
98        $query = new \BO\Zmsbackend\Config\Repository\Config(\BO\Zmsbackend\Query\Base::DELETE);
99        $query->addConditionName($property);
100
101        if (App::$cache && App::$cache->has('config')) {
102            App::$cache->delete('config');
103        }
104
105        return $this->deleteItem($query);
106    }
107
108    protected function fetchData(string $querySql): Entity
109    {
110        $splittedHash = array();
111        $dataList = $this->getReader()->fetchAll($querySql);
112        foreach ($dataList as $data) {
113            $splittedHash[$data['name']] = $data['value'];
114        }
115        return new Entity($splittedHash);
116    }
117
118    protected function getSpecifiedValue($value): int|string
119    {
120        if (is_bool($value)) {
121            return ($value) ? 1 : 0;
122        }
123        return trim($value);
124    }
125}