Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
56 / 63 |
|
69.23% |
9 / 13 |
CRAP | |
0.00% |
0 / 1 |
| AbstractAccess | |
88.89% |
56 / 63 |
|
69.23% |
9 / 13 |
28.00 | |
0.00% |
0 / 1 |
| addAccessInstanceLocale | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getInstanceCompatibilities | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| __call | |
96.77% |
30 / 31 |
|
0.00% |
0 / 1 |
9 | |||
| getInstanceOnName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| from | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
4.59 | |||
| fromAuthority | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromBorough | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromLink | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fromLocation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromOffice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromSetting | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromTopic | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package 115Mandant |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsdldb; |
| 9 | |
| 10 | /** |
| 11 | * Common methods shared by access classes |
| 12 | */ |
| 13 | class AbstractAccess |
| 14 | { |
| 15 | protected static $showDeprecated = false; |
| 16 | |
| 17 | protected $accessInstance = array( |
| 18 | 'de' => array( |
| 19 | 'Authority' => null, |
| 20 | 'Borough' => null, |
| 21 | 'Link' => null, |
| 22 | 'Location' => null, |
| 23 | 'Office' => null, |
| 24 | 'Service' => null, |
| 25 | 'Setting' => null, |
| 26 | 'Topic' => null |
| 27 | ), |
| 28 | 'en' => array( |
| 29 | 'Authority' => null, |
| 30 | 'Borough' => null, |
| 31 | 'Link' => null, |
| 32 | 'Location' => null, |
| 33 | 'Office' => null, |
| 34 | 'Service' => null, |
| 35 | 'Setting' => null, |
| 36 | 'Topic' => null |
| 37 | ) |
| 38 | ); |
| 39 | |
| 40 | protected static $accessInstanceTypes = [ |
| 41 | 'Authority' => null, |
| 42 | 'Borough' => null, |
| 43 | 'Link' => null, |
| 44 | 'Location' => null, |
| 45 | 'Office' => null, |
| 46 | 'Service' => null, |
| 47 | 'Setting' => null, |
| 48 | 'Topic' => null |
| 49 | ]; |
| 50 | |
| 51 | public function addAccessInstanceLocale($locale = 'de') |
| 52 | { |
| 53 | if (!isset($this->accessInstance[$locale])) { |
| 54 | $this->accessInstance[$locale] = static::$accessInstanceTypes; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | private function getInstanceCompatibilities() |
| 60 | { |
| 61 | $accessInstance = $this->accessInstance['de']; |
| 62 | $accessInstance['Authorities'] = $accessInstance['Authority']; |
| 63 | $accessInstance['Boroughs'] = $accessInstance['Borough']; |
| 64 | $accessInstance['Offices'] = $accessInstance['Office']; |
| 65 | $accessInstance['Settings'] = $accessInstance['Setting']; |
| 66 | $accessInstance['Topics'] = $accessInstance['Topic']; |
| 67 | $accessInstance['Locations'] = $accessInstance['Location']; |
| 68 | $accessInstance['Services'] = $accessInstance['Service']; |
| 69 | return $accessInstance; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * find matching function in instance |
| 74 | * |
| 75 | * @return Mixed |
| 76 | */ |
| 77 | public function __call($functionName, $functionArguments) |
| 78 | { |
| 79 | if (self::$showDeprecated) { |
| 80 | trigger_error("Deprecated access function: $functionName"); |
| 81 | } |
| 82 | $actionType = 'none'; |
| 83 | $instanceName = 'Missing'; |
| 84 | $actionName = 'Nothing'; |
| 85 | if (0 === strpos($functionName, 'fetch')) { |
| 86 | $actionType = 'fetch'; |
| 87 | $instanceName = $this->getInstanceOnName($functionName, 5); |
| 88 | $actionName = substr($functionName, 5 + strlen($instanceName ?? '')); |
| 89 | if (! $actionName) { |
| 90 | $actionName = 'Id'; |
| 91 | } |
| 92 | } elseif (0 === strpos($functionName, 'search')) { |
| 93 | $actionType = 'search'; |
| 94 | $instanceName = $this->getInstanceOnName($functionName, 6); |
| 95 | $actionName = substr($functionName, 6 + strlen($instanceName ?? '')); |
| 96 | if (! $actionName) { |
| 97 | $actionType = "read"; |
| 98 | $actionName = 'SearchResultList'; |
| 99 | } |
| 100 | } |
| 101 | $accessInstance = $this->getInstanceCompatibilities(); |
| 102 | if ( |
| 103 | $instanceName |
| 104 | && $instanceName != 'Missing' |
| 105 | && method_exists($accessInstance[$instanceName], $actionType . $actionName) |
| 106 | ) { |
| 107 | $accessInstance[$instanceName]->setAccessInstance($this); |
| 108 | return call_user_func_array(array( |
| 109 | $accessInstance[$instanceName], |
| 110 | $actionType . $actionName |
| 111 | ), $functionArguments); |
| 112 | } |
| 113 | $classname = get_class($this); |
| 114 | throw new Exception( |
| 115 | "Unknown access function or instance: $classname::$functionName ($instanceName::$actionType$actionName)" |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * |
| 121 | * @return String InstanceName |
| 122 | */ |
| 123 | protected function getInstanceOnName($name, $position = 0) |
| 124 | { |
| 125 | foreach (array_keys($this->getInstanceCompatibilities()) as $instanceName) { |
| 126 | if ($position === strpos($name, $instanceName)) { |
| 127 | return $instanceName; |
| 128 | } |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | protected function from($instanceName, $locale = 'de') |
| 134 | { |
| 135 | if (array_key_exists($instanceName, $this->accessInstance[$locale])) { |
| 136 | $instance = $this->accessInstance[$locale][$instanceName]; |
| 137 | if ($instance instanceof \BO\Zmsdldb\File\Base) { |
| 138 | return $instance; |
| 139 | } |
| 140 | if (null === $instance) { |
| 141 | throw new Exception("Instance for accessing $instanceName ($locale) is not initialized"); |
| 142 | } |
| 143 | throw new Exception("Instance for accessing $instanceName failed"); |
| 144 | } |
| 145 | echo '<pre>' . print_r($this->accessInstance, 1) . '</pre>'; |
| 146 | //exit; |
| 147 | throw new Exception("Locale for accessing $instanceName does not exists"); |
| 148 | } |
| 149 | |
| 150 | public function fromAuthority($locale = 'de') |
| 151 | { |
| 152 | return $this->from('Authority', $locale); |
| 153 | } |
| 154 | |
| 155 | public function fromBorough() |
| 156 | { |
| 157 | return $this->from('Borough'); |
| 158 | } |
| 159 | |
| 160 | public function fromLink($locale = 'de') |
| 161 | { |
| 162 | return $this->from('Link', $locale); |
| 163 | } |
| 164 | |
| 165 | public function fromLocation($locale = 'de') |
| 166 | { |
| 167 | return $this->from('Location', $locale); |
| 168 | } |
| 169 | |
| 170 | public function fromOffice() |
| 171 | { |
| 172 | return $this->from('Office'); |
| 173 | } |
| 174 | |
| 175 | public function fromService($locale = 'de') |
| 176 | { |
| 177 | return $this->from('Service', $locale); |
| 178 | } |
| 179 | |
| 180 | public function fromSetting() |
| 181 | { |
| 182 | return $this->from('Setting'); |
| 183 | } |
| 184 | |
| 185 | public function fromTopic($locale = 'de') |
| 186 | { |
| 187 | return $this->from('Topic', $locale); |
| 188 | } |
| 189 | } |