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