Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.00% covered (warning)
84.00%
63 / 75
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractAccess
84.00% covered (warning)
84.00%
63 / 75
69.23% covered (warning)
69.23%
9 / 13
32.44
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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromBorough
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromLink
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 fromLocation
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromOffice
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromService
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromSetting
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 fromTopic
100.00% covered (success)
100.00%
2 / 2
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 bool $showDeprecated = false;
16
17    protected mixed $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 mixed $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(string $locale = 'de'): void
55    {
56        if (!isset($this->accessInstance[$locale])) {
57            $this->accessInstance[$locale] = static::$accessInstanceTypes;
58        }
59    }
60
61
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    }
74
75    /**
76     * find matching function in instance
77     *
78     * @return Mixed
79     */
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    }
124
125    /**
126     * @return string|null InstanceName
127     */
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    }
137
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    }
158
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    }
165
166    public function fromBorough(): File\Borough
167    {
168        /** @var File\Borough $instance */
169        $instance = $this->from('Borough');
170        return $instance;
171    }
172
173    /** @psalm-api */
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    }
180
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    }
187
188    public function fromOffice(): File\Office
189    {
190        /** @var File\Office $instance */
191        $instance = $this->from('Office');
192        return $instance;
193    }
194
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    }
201
202    public function fromSetting(): File\Setting
203    {
204        /** @var File\Setting $instance */
205        $instance = $this->from('Setting');
206        return $instance;
207    }
208
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    }
215}