Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.58% covered (warning)
83.58%
56 / 67
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractAccess
83.58% covered (warning)
83.58%
56 / 67
69.23% covered (warning)
69.23%
9 / 13
32.72
0.00% covered (danger)
0.00%
0 / 1
 addAccessInstanceLocale
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getInstanceCompatibilities
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 __call
96.77% covered (success)
96.77%
30 / 31
0.00% covered (danger)
0.00%
0 / 1
9
 getInstanceOnName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 from
46.15% covered (danger)
46.15%
6 / 13
0.00% covered (danger)
0.00%
0 / 1
11.62
 fromAuthority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromBorough
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromLink
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fromLocation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromOffice
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromSetting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromTopic
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package 115Mandant
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb;
9
10/**
11 * Common methods shared by access classes
12 */
13class 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    /**
52     * @psalm-api
53     */
54    public function addAccessInstanceLocale($locale = 'de'): void
55    {
56        if (!isset($this->accessInstance[$locale])) {
57            $this->accessInstance[$locale] = static::$accessInstanceTypes;
58        }
59    }
60
61
62    private function getInstanceCompatibilities()
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    }
74
75    /**
76     * find matching function in instance
77     *
78     * @return Mixed
79     */
80    public function __call($functionName, $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            $instanceName = $this->getInstanceOnName($functionName, 5);
91            $actionName = substr($functionName, 5 + strlen($instanceName ?? ''));
92            if (! $actionName) {
93                $actionName = 'Id';
94            }
95        } elseif (0 === strpos($functionName, 'search')) {
96            $actionType = 'search';
97            $instanceName = $this->getInstanceOnName($functionName, 6);
98            $actionName = substr($functionName, 6 + strlen($instanceName ?? ''));
99            if (! $actionName) {
100                $actionType = "read";
101                $actionName = 'SearchResultList';
102            }
103        }
104        $accessInstance = $this->getInstanceCompatibilities();
105        if (
106            $instanceName
107            && $instanceName != 'Missing'
108            && method_exists($accessInstance[$instanceName], $actionType . $actionName)
109        ) {
110            $accessInstance[$instanceName]->setAccessInstance($this);
111            return call_user_func_array(array(
112                $accessInstance[$instanceName],
113                $actionType . $actionName
114            ), $functionArguments);
115        }
116        $classname = get_class($this);
117        throw new Exception(
118            "Unknown access function or instance: $classname::$functionName ($instanceName::$actionType$actionName)"
119        );
120    }
121
122    /**
123     * @return string InstanceName
124     *
125     */
126    protected function getInstanceOnName($name, int $position = 0)
127    {
128        foreach (array_keys($this->getInstanceCompatibilities()) as $instanceName) {
129            if ($position === strpos($name, $instanceName)) {
130                return $instanceName;
131            }
132        }
133        return null;
134    }
135
136    protected function from(string $instanceName, $locale = 'de'): File\Base
137    {
138        if (array_key_exists($instanceName, $this->accessInstance[$locale])) {
139            $instance = $this->accessInstance[$locale][$instanceName];
140            if ($instance instanceof \BO\Zmsdldb\File\Base) {
141                return $instance;
142            }
143            if (null === $instance) {
144                throw new Exception("Instance for accessing $instanceName ($locale) is not initialized");
145            }
146            throw new Exception("Instance for accessing $instanceName failed");
147        }
148        if (class_exists('\App', false) && isset(\App::$log)) {
149            \App::$log->error('DLDB access locale missing', [
150                'instance' => $instanceName,
151                'locale' => $locale,
152            ]);
153        }
154        throw new Exception("Locale for accessing $instanceName does not exists");
155    }
156
157    public function fromAuthority($locale = 'de')
158    {
159        return $this->from('Authority', $locale);
160    }
161
162    public function fromBorough()
163    {
164        return $this->from('Borough');
165    }
166
167    /** @psalm-api */
168    public function fromLink($locale = 'de')
169    {
170        return $this->from('Link', $locale);
171    }
172
173    public function fromLocation($locale = 'de')
174    {
175        return $this->from('Location', $locale);
176    }
177
178    public function fromOffice()
179    {
180        return $this->from('Office');
181    }
182
183    public function fromService($locale = 'de')
184    {
185        return $this->from('Service', $locale);
186    }
187
188    public function fromSetting()
189    {
190        return $this->from('Setting');
191    }
192
193    public function fromTopic($locale = 'de')
194    {
195        return $this->from('Topic', $locale);
196    }
197}