Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
42 / 42 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Config | |
100.00% |
42 / 42 |
|
100.00% |
7 / 7 |
17 | |
100.00% |
1 / 1 |
readEntity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
updateEntity | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
7 | |||
readProperty | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
replaceProperty | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
deleteProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
fetchData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getSpecifiedValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Config as Entity; |
6 | |
7 | class Config extends Base |
8 | { |
9 | /** |
10 | * |
11 | * @return \BO\Zmsentities\Config |
12 | */ |
13 | public function readEntity() |
14 | { |
15 | $query = Query\Config::QUERY_SELECT; |
16 | $config = $this->fetchData($query); |
17 | return $config; |
18 | } |
19 | |
20 | public function updateEntity(Entity $config) |
21 | { |
22 | $compareEntity = $this->readEntity(); |
23 | $result = false; |
24 | $query = new Query\Config(Query\Base::REPLACE); |
25 | foreach ($config as $key => $item) { |
26 | if (is_array($item)) { |
27 | foreach ($item as $itemName => $itemValue) { |
28 | if ($itemValue && $compareEntity->getPreference($key, $itemName) != $itemValue) { |
29 | $query->addValues(array( |
30 | 'name' => $key . '__' . $itemName, |
31 | 'value' => $this->getSpecifiedValue($itemValue), |
32 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
33 | )); |
34 | $result = $this->writeItem($query); |
35 | } |
36 | } |
37 | } else { |
38 | $query->addValues(array( |
39 | 'name' => $key, |
40 | 'value' => $this->getSpecifiedValue($item), |
41 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
42 | )); |
43 | $result = $this->writeItem($query); |
44 | } |
45 | } |
46 | return ($result) ? $this->readEntity() : null; |
47 | } |
48 | |
49 | public function readProperty($property, $forUpdate = false) |
50 | { |
51 | $sql = Query\Config::QUERY_SELECT_PROPERTY; |
52 | if ($forUpdate) { |
53 | $sql .= " FOR UPDATE"; |
54 | } |
55 | return $this->fetchValue($sql, [$property]); |
56 | } |
57 | |
58 | public function replaceProperty($property, $value) |
59 | { |
60 | return $this->perform(Query\Config::QUERY_REPLACE_PROPERTY, [ |
61 | 'property' => $property, |
62 | 'value' => $value, |
63 | ]); |
64 | } |
65 | |
66 | /** |
67 | * remove config data |
68 | * |
69 | * |
70 | * @return Resource Status |
71 | */ |
72 | public function deleteProperty($property) |
73 | { |
74 | $query = new Query\Config(Query\Base::DELETE); |
75 | $query->addConditionName($property); |
76 | return $this->deleteItem($query); |
77 | } |
78 | |
79 | protected function fetchData($querySql) |
80 | { |
81 | $splittedHash = array(); |
82 | $dataList = $this->getReader()->fetchAll($querySql); |
83 | foreach ($dataList as $data) { |
84 | $splittedHash[$data['name']] = $data['value']; |
85 | } |
86 | return new Entity($splittedHash); |
87 | } |
88 | |
89 | protected function getSpecifiedValue($value) |
90 | { |
91 | if (is_bool($value)) { |
92 | return ($value) ? 1 : 0; |
93 | } |
94 | return trim($value); |
95 | } |
96 | } |