Lines
63.63%
28 / 44
Functions and Methods
45.45%
5 / 11
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Base | 63.63% | 28 / 44 | 45.45% | 5 / 11 | 48.44 | 0.00% | 0 / 1 | |||
| parseData | n/a | 0 / 0 | n/a | 0 / 0 | 0 | |||||
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| readDataFile | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 5.01 | |||||
| getDataAsArray | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| getHash | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| getData | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| loadData | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| getItemList | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| setItemList | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | |||||
| fetchId | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| setAccessInstance | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| access | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 2.15 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @package 115Mandant | |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG | |
| 6 | **/ | |
| 7 | ||
| 8 | namespace BO\Zmsdldb\File; | |
| 9 | ||
| 10 | use BO\Zmsdldb\Exception; | |
| 11 | ||
| 12 | /** | |
| 13 | * Common methods shared by access classes | |
| 14 | * | |
| 15 | * @template TCollection of \BO\Zmsdldb\Collection\Base | |
| 16 | * @template TEntity of \BO\Zmsdldb\Entity\Base | |
| 17 | */ | |
| 18 | abstract class Base | |
| 19 | { | |
| 20 | protected mixed $data = []; | |
| 21 | /** | |
| 22 | * lazy loaded item list, use getItemList() to access this | |
| 23 | * | |
| 24 | * @var TCollection|null $itemList | |
| 25 | */ | |
| 26 | private $itemList = null; | |
| 27 | ||
| 28 | /** | |
| 29 | * @var String $dataFile | |
| 30 | */ | |
| 31 | protected $dataFile = ''; | |
| 32 | ||
| 33 | /** | |
| 34 | * @var String $locale Format like 'en' | |
| 35 | * | |
| 36 | */ | |
| 37 | protected $locale = 'de'; | |
| 38 | ||
| 39 | /** | |
| 40 | * @var \BO\Zmsdldb\AbstractAccess|null $accessInstance | |
| 41 | */ | |
| 42 | private $accessInstance = null; | |
| 43 | ||
| 44 | /** | |
| 45 | * @return TCollection | |
| 46 | */ | |
| 47 | abstract protected function parseData(mixed $data); | |
| 48 | ||
| 49 | public function __construct(mixed $dataFile, string $locale = "de") | |
| 50 | { | |
| 51 | $this->dataFile = $dataFile; | |
| 52 | $this->locale = $locale; | |
| 53 | } | |
| 54 | ||
| 55 | public function readDataFile(): mixed | |
| 56 | { | |
| 57 | if (empty($this->data)) { | |
| 58 | $jsonFile = $this->dataFile; | |
| 59 | if (!is_readable($jsonFile)) { | |
| 60 | throw new Exception("Cannot read $jsonFile"); | |
| 61 | } | |
| 62 | $json = file_get_contents($jsonFile); | |
| 63 | if ($json === false) { | |
| 64 | throw new Exception("Cannot read $jsonFile"); | |
| 65 | } | |
| 66 | $data = json_decode($json, true); | |
| 67 | if (!$data) { | |
| 68 | throw new Exception("Could not decide $jsonFile"); | |
| 69 | } | |
| 70 | $this->data = $data; | |
| 71 | } | |
| 72 | return $this->data; | |
| 73 | } | |
| 74 | ||
| 75 | /** @psalm-api */ | |
| 76 | public function getDataAsArray(): mixed | |
| 77 | { | |
| 78 | try { | |
| 79 | $data = $this->readDataFile(); | |
| 80 | ||
| 81 | return $data['data']; | |
| 82 | } catch (\Exception $e) { | |
| 83 | throw $e; | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | /** @psalm-api */ | |
| 88 | public function getHash(): mixed | |
| 89 | { | |
| 90 | try { | |
| 91 | $data = $this->readDataFile(); | |
| 92 | ||
| 93 | return $data['hash']; | |
| 94 | } catch (\Exception $e) { | |
| 95 | throw $e; | |
| 96 | } | |
| 97 | } | |
| 98 | ||
| 99 | /** @psalm-api */ | |
| 100 | public function getData(): mixed | |
| 101 | { | |
| 102 | try { | |
| 103 | $data = $this->readDataFile(); | |
| 104 | ||
| 105 | return $data; | |
| 106 | } catch (\Exception $e) { | |
| 107 | throw $e; | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * @return void | |
| 113 | */ | |
| 114 | public function loadData() | |
| 115 | { | |
| 116 | try { | |
| 117 | $data = $this->readDataFile(); | |
| 118 | $this->itemList = $this->parseData($data); | |
| 119 | } catch (\Exception $e) { | |
| 120 | throw $e; | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * @return TCollection | |
| 126 | */ | |
| 127 | public function getItemList() | |
| 128 | { | |
| 129 | if (null === $this->itemList) { | |
| 130 | $this->loadData(); | |
| 131 | } | |
| 132 | return $this->itemList; | |
| 133 | } | |
| 134 | ||
| 135 | protected function setItemList(\BO\Zmsdldb\Collection\Base $list): static | |
| 136 | { | |
| 137 | /** @var TCollection $list */ | |
| 138 | $this->itemList = $list; | |
| 139 | return $this; | |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * @return TEntity|false | |
| 144 | */ | |
| 145 | public function fetchId(string $itemId) | |
| 146 | { | |
| 147 | $itemList = $this->getItemList(); | |
| 148 | ||
| 149 | if (! $itemId || !$itemList->offsetExists($itemId)) { | |
| 150 | return false; | |
| 151 | } | |
| 152 | ||
| 153 | /** @var TEntity $item */ | |
| 154 | $item = $itemList[$itemId]; | |
| 155 | return $item; | |
| 156 | } | |
| 157 | ||
| 158 | public function setAccessInstance(\BO\Zmsdldb\AbstractAccess $accessInstance): void | |
| 159 | { | |
| 160 | $this->accessInstance = $accessInstance; | |
| 161 | } | |
| 162 | ||
| 163 | public function access(): \BO\Zmsdldb\AbstractAccess | |
| 164 | { | |
| 165 | if (!$this->accessInstance instanceof \BO\Zmsdldb\AbstractAccess) { | |
| 166 | throw new Exception('Access instance is not initialized'); | |
| 167 | } | |
| 168 | return $this->accessInstance; | |
| 169 | } | |
| 170 | } |