Lines
56.48%
74 / 131
Methods
30.00%
9 / 30
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | ||
| connectElasticSearch | 0.00% | 0 / 6 | 0.00% | 0 / 1 | 2 | ||
| getIndex | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 6 | ||
| getConnection | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 6 | ||
| loadLocations | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| loadServices | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| loadTopics | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 2 | ||
| loadSettings | 0.00% | 0 / 7 | 0.00% | 0 / 1 | 2 | ||
| loadAuthorities | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] loadFromPath | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 2.00 | ||
| [BO\Zmsdldb\FileAccess] loadLocationsFromPathByLocale | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] loadServicesFromPathByLocale | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] loadTopicsFromPathByLocale | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] loadAuthoritiesFromPathByLocale | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] loadSettingsFromPath | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] fetchTopicServicesList | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\FileAccess] fetchLocationListByOffice | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\AbstractAccess] addAccessInstanceLocale | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | ||
| [BO\Zmsdldb\AbstractAccess] getInstanceCompatibilities | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] __call | 96.77% | 30 / 31 | 0.00% | 0 / 1 | 9 | ||
| [BO\Zmsdldb\AbstractAccess] getInstanceOnName | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsdldb\AbstractAccess] from | 46.15% | 6 / 13 | 0.00% | 0 / 1 | 11.62 | ||
| [BO\Zmsdldb\AbstractAccess] fromAuthority | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromBorough | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromLink | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsdldb\AbstractAccess] fromLocation | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromOffice | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromService | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromSetting | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsdldb\AbstractAccess] fromTopic | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| 16 | class ElasticAccess extends FileAccess | |
| 17 | { | |
| 18 | /** | |
| 19 | * The client used to talk to elastic search. | |
| 20 | * | |
| 21 | * @var \Elastica\Client|null | |
| 22 | */ | |
| 23 | protected $connection = null; | |
| 24 | ||
| 25 | /** | |
| 26 | * Index from elastic search | |
| 27 | * | |
| 28 | * @var \Elastica\Index|null $index | |
| 29 | */ | |
| 30 | protected $index = null; | |
| 31 | ||
| 32 | /** | |
| 33 | * | |
| 34 | * @return self | |
| 35 | */ | |
| 36 | public function __construct(mixed $index = null, string $host = 'localhost', string $port = '9200', string $transport = 'Http') | |
| 37 | { | |
| 38 | if ($index) { | |
| 39 | $this->connectElasticSearch($index, $host, $port, $transport); | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | public function connectElasticSearch(mixed $index, string $host = 'localhost', string $port = '9200', string $transport = 'Http'): void | |
| 44 | { | |
| 45 | $this->connection = new \Elastica\Client(array( | |
| 46 | 'host' => $host, | |
| 47 | 'port' => $port, | |
| 48 | 'transport' => $transport | |
| 49 | )); | |
| 50 | $this->index = $this->getConnection()->getIndex($index); | |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * | |
| 55 | * @return \Elastica\Index | |
| 56 | * @psalm-api | |
| 57 | */ | |
| 58 | public function getIndex() | |
| 59 | { | |
| 60 | if (!$this->index instanceof \Elastica\Index) { | |
| 61 | throw new Exception('ElasticSearch index is not initialized'); | |
| 62 | } | |
| 63 | return $this->index; | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * | |
| 68 | * @return \Elastica\Client | |
| 69 | */ | |
| 70 | protected function getConnection() | |
| 71 | { | |
| 72 | if (!$this->connection instanceof \Elastica\Client) { | |
| 73 | throw new Exception('ElasticSearch connection is not initialized'); | |
| 74 | } | |
| 75 | return $this->connection; | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * | |
| 80 | * @return self | |
| 81 | */ | |
| 82 | #[\Override] | |
| 83 | public function loadLocations(mixed $locationJson, string $locale = 'de') | |
| 84 | { | |
| 85 | $this->accessInstance[$locale]['Location'] = new Elastic\Location($locationJson, $locale); | |
| 86 | $this->accessInstance[$locale]['Location']->setAccessInstance($this); | |
| 87 | return $this; | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * | |
| 92 | * @return self | |
| 93 | */ | |
| 94 | #[\Override] | |
| 95 | public function loadServices(mixed $serviceJson, string $locale = 'de') | |
| 96 | { | |
| 97 | $this->accessInstance[$locale]['Service'] = new Elastic\Service($serviceJson, $locale); | |
| 98 | $this->accessInstance[$locale]['Service']->setAccessInstance($this); | |
| 99 | return $this; | |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * | |
| 104 | * @return self | |
| 105 | */ | |
| 106 | #[\Override] | |
| 107 | public function loadTopics(mixed $topicJson, string $locale = 'de') | |
| 108 | { | |
| 109 | $this->accessInstance[$locale]['Topic'] = new Elastic\Topic($topicJson, $locale); | |
| 110 | $this->accessInstance[$locale]['Topic']->setAccessInstance($this); | |
| 111 | $this->accessInstance[$locale]['Link'] = new Elastic\Link($topicJson, $locale); | |
| 112 | $this->accessInstance[$locale]['Link']->setAccessInstance($this); | |
| 113 | return $this; | |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * | |
| 118 | * @return self | |
| 119 | */ | |
| 120 | #[\Override] | |
| 121 | public function loadSettings(mixed $settingsJson) | |
| 122 | { | |
| 123 | $this->accessInstance['de']['Setting'] = new Elastic\Setting($settingsJson); | |
| 124 | $this->accessInstance['de']['Setting']->setAccessInstance($this); | |
| 125 | $this->accessInstance['de']['Office'] = new Elastic\Office($settingsJson); | |
| 126 | $this->accessInstance['de']['Office']->setAccessInstance($this); | |
| 127 | $this->accessInstance['de']['Borough'] = new Elastic\Borough($settingsJson); | |
| 128 | $this->accessInstance['de']['Borough']->setAccessInstance($this); | |
| 129 | return $this; | |
| 130 | } | |
| 131 | ||
| 132 | ||
| 133 | /** | |
| 134 | * | |
| 135 | * @return self | |
| 136 | */ | |
| 137 | #[\Override] | |
| 138 | public function loadAuthorities(mixed $authorityJson, string $locale = 'de') | |
| 139 | { | |
| 140 | $this->accessInstance[$locale]['Authority'] = new Elastic\Authority($authorityJson, $locale); | |
| 141 | $this->accessInstance[$locale]['Authority']->setAccessInstance($this); | |
| 142 | return $this; | |
| 143 | } | |
| 144 | } |
Inherited from BO\Zmsdldb\FileAccess
| 50 | public function loadFromPath(string $path) | |
| 51 | { | |
| 52 | if (!is_dir($path)) { | |
| 53 | throw new Exception("Could not read directory $path"); | |
| 54 | } | |
| 55 | $this->loadAuthorities($path . DIRECTORY_SEPARATOR . 'authority_de.json', 'de'); | |
| 56 | $this->loadAuthorities($path . DIRECTORY_SEPARATOR . 'authority_de.json', 'en'); | |
| 57 | $this->loadLocations($path . DIRECTORY_SEPARATOR . 'locations_de.json', 'de'); | |
| 58 | $this->loadLocations($path . DIRECTORY_SEPARATOR . 'locations_en.json', 'en'); | |
| 59 | $this->loadServices($path . DIRECTORY_SEPARATOR . 'services_de.json', 'de'); | |
| 60 | $this->loadServices($path . DIRECTORY_SEPARATOR . 'services_en.json', 'en'); | |
| 61 | $this->loadSettings($path . DIRECTORY_SEPARATOR . 'settings.json'); | |
| 62 | $this->loadTopics($path . DIRECTORY_SEPARATOR . 'topic_de.json', 'de'); | |
| 63 | $this->loadTopics($path . DIRECTORY_SEPARATOR . 'topic_de.json', 'en'); | |
| 64 | return $this; | |
| 65 | } |
| 70 | public function loadLocationsFromPathByLocale(mixed $path, mixed $locale): void | |
| 71 | { | |
| 72 | $this->loadLocations($path . DIRECTORY_SEPARATOR . 'locations_' . $locale . '.json', $locale); | |
| 73 | } |
| 78 | public function loadServicesFromPathByLocale(mixed $path, mixed $locale): void | |
| 79 | { | |
| 80 | $this->loadServices($path . DIRECTORY_SEPARATOR . 'services_' . $locale . '.json', $locale); | |
| 81 | } |
| 86 | public function loadTopicsFromPathByLocale(mixed $path, mixed $locale): void | |
| 87 | { | |
| 88 | $this->loadTopics($path . DIRECTORY_SEPARATOR . 'topic_' . $locale . '.json', $locale); | |
| 89 | } |
| 94 | public function loadAuthoritiesFromPathByLocale(mixed $path, mixed $locale): void | |
| 95 | { | |
| 96 | $this->loadAuthorities($path . DIRECTORY_SEPARATOR . 'authority_' . $locale . '.json', $locale); | |
| 97 | } |
| 102 | public function loadSettingsFromPath(mixed $path): void | |
| 103 | { | |
| 104 | $this->loadSettings($path . DIRECTORY_SEPARATOR . 'settings.json'); | |
| 105 | } |
| 173 | public function fetchTopicServicesList(mixed $topic_path): mixed | |
| 174 | { | |
| 175 | trigger_error("Deprecated function fetchTopicServicesList, use fromService()->fetchTopic()"); | |
| 176 | /** @psalm-suppress UndefinedMethod */ | |
| 177 | return $this->fromService()->fetchTopicPath($topic_path); | |
| 178 | } |
| 187 | public function fetchLocationListByOffice(string|false $officepath = false) | |
| 188 | { | |
| 189 | trigger_error("Deprecated function fetchLocationListByOffice, use fromAuthority()->fetchOffice()"); | |
| 190 | /** @psalm-suppress UndefinedMethod */ | |
| 191 | return $this->fromAuthority()->fetchOffice($officepath); | |
| 192 | } |
Inherited from BO\Zmsdldb\AbstractAccess
| 54 | public function addAccessInstanceLocale(string $locale = 'de'): void | |
| 55 | { | |
| 56 | if (!isset($this->accessInstance[$locale])) { | |
| 57 | $this->accessInstance[$locale] = static::$accessInstanceTypes; | |
| 58 | } | |
| 59 | } |
| 62 | private function getInstanceCompatibilities(): mixed | |
| 63 | { | |
| 64 | $accessInstance = $this->accessInstance['de']; | |
| 65 | $accessInstance['Authorities'] = $accessInstance['Authority']; | |
| 66 | $accessInstance['Boroughs'] = $accessInstance['Borough']; | |
| 67 | $accessInstance['Offices'] = $accessInstance['Office']; | |
| 68 | $accessInstance['Settings'] = $accessInstance['Setting']; | |
| 69 | $accessInstance['Topics'] = $accessInstance['Topic']; | |
| 70 | $accessInstance['Locations'] = $accessInstance['Location']; | |
| 71 | $accessInstance['Services'] = $accessInstance['Service']; | |
| 72 | return $accessInstance; | |
| 73 | } |
| 80 | public function __call(string $functionName, array $functionArguments) | |
| 81 | { | |
| 82 | if (self::$showDeprecated) { | |
| 83 | trigger_error("Deprecated access function: $functionName"); | |
| 84 | } | |
| 85 | $actionType = 'none'; | |
| 86 | $instanceName = 'Missing'; | |
| 87 | $actionName = 'Nothing'; | |
| 88 | if (0 === strpos($functionName, 'fetch')) { | |
| 89 | $actionType = 'fetch'; | |
| 90 | /** @var string|null $instanceName */ | |
| 91 | $instanceName = $this->getInstanceOnName($functionName, 5); | |
| 92 | $actionName = substr($functionName, 5 + strlen($instanceName ?? '')); | |
| 93 | if (! $actionName) { | |
| 94 | $actionName = 'Id'; | |
| 95 | } | |
| 96 | } elseif (0 === strpos($functionName, 'search')) { | |
| 97 | $actionType = 'search'; | |
| 98 | /** @var string|null $instanceName */ | |
| 99 | $instanceName = $this->getInstanceOnName($functionName, 6); | |
| 100 | $actionName = substr($functionName, 6 + strlen($instanceName ?? '')); | |
| 101 | if (! $actionName) { | |
| 102 | $actionType = "read"; | |
| 103 | $actionName = 'SearchResultList'; | |
| 104 | } | |
| 105 | } | |
| 106 | $accessInstance = $this->getInstanceCompatibilities(); | |
| 107 | /** @psalm-suppress RiskyTruthyFalsyComparison */ | |
| 108 | if ( | |
| 109 | $instanceName | |
| 110 | && $instanceName != 'Missing' | |
| 111 | && method_exists($accessInstance[$instanceName], $actionType . $actionName) | |
| 112 | ) { | |
| 113 | $accessInstance[$instanceName]->setAccessInstance($this); | |
| 114 | return call_user_func_array(array( | |
| 115 | $accessInstance[$instanceName], | |
| 116 | $actionType . $actionName | |
| 117 | ), $functionArguments); | |
| 118 | } | |
| 119 | $classname = get_class($this); | |
| 120 | throw new Exception( | |
| 121 | "Unknown access function or instance: $classname::$functionName ($instanceName::$actionType$actionName)" | |
| 122 | ); | |
| 123 | } |
| 128 | protected function getInstanceOnName(mixed $name, int $position = 0) | |
| 129 | { | |
| 130 | foreach (array_keys($this->getInstanceCompatibilities()) as $instanceName) { | |
| 131 | if ($position === strpos($name, $instanceName)) { | |
| 132 | return $instanceName; | |
| 133 | } | |
| 134 | } | |
| 135 | return null; | |
| 136 | } |
| 138 | protected function from(string $instanceName, string $locale = 'de'): File\Base | |
| 139 | { | |
| 140 | if (array_key_exists($instanceName, $this->accessInstance[$locale])) { | |
| 141 | $instance = $this->accessInstance[$locale][$instanceName]; | |
| 142 | if ($instance instanceof \BO\Zmsdldb\File\Base) { | |
| 143 | return $instance; | |
| 144 | } | |
| 145 | if (null === $instance) { | |
| 146 | throw new Exception("Instance for accessing $instanceName ($locale) is not initialized"); | |
| 147 | } | |
| 148 | throw new Exception("Instance for accessing $instanceName failed"); | |
| 149 | } | |
| 150 | if (class_exists('\App', false) && isset(\App::$log)) { | |
| 151 | \App::$log->error('DLDB access locale missing', [ | |
| 152 | 'instance' => $instanceName, | |
| 153 | 'locale' => $locale, | |
| 154 | ]); | |
| 155 | } | |
| 156 | throw new Exception("Locale for accessing $instanceName does not exists"); | |
| 157 | } |
| 159 | public function fromAuthority(string $locale = 'de'): File\Authority | |
| 160 | { | |
| 161 | /** @var File\Authority $instance */ | |
| 162 | $instance = $this->from('Authority', $locale); | |
| 163 | return $instance; | |
| 164 | } |
| 166 | public function fromBorough(): File\Borough | |
| 167 | { | |
| 168 | /** @var File\Borough $instance */ | |
| 169 | $instance = $this->from('Borough'); | |
| 170 | return $instance; | |
| 171 | } |
| 174 | public function fromLink(string $locale = 'de'): File\Link | |
| 175 | { | |
| 176 | /** @var File\Link $instance */ | |
| 177 | $instance = $this->from('Link', $locale); | |
| 178 | return $instance; | |
| 179 | } |
| 181 | public function fromLocation(string $locale = 'de'): File\Location | |
| 182 | { | |
| 183 | /** @var File\Location $instance */ | |
| 184 | $instance = $this->from('Location', $locale); | |
| 185 | return $instance; | |
| 186 | } |
| 188 | public function fromOffice(): File\Office | |
| 189 | { | |
| 190 | /** @var File\Office $instance */ | |
| 191 | $instance = $this->from('Office'); | |
| 192 | return $instance; | |
| 193 | } |
| 195 | public function fromService(string $locale = 'de'): File\Service | |
| 196 | { | |
| 197 | /** @var File\Service $instance */ | |
| 198 | $instance = $this->from('Service', $locale); | |
| 199 | return $instance; | |
| 200 | } |
| 202 | public function fromSetting(): File\Setting | |
| 203 | { | |
| 204 | /** @var File\Setting $instance */ | |
| 205 | $instance = $this->from('Setting'); | |
| 206 | return $instance; | |
| 207 | } |
| 209 | public function fromTopic(string $locale = 'de'): File\Topic | |
| 210 | { | |
| 211 | /** @var File\Topic $instance */ | |
| 212 | $instance = $this->from('Topic', $locale); | |
| 213 | return $instance; | |
| 214 | } |