getServiceClassFromNamespaces()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 3
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Core\Business\Model\Locator\Proxy;
5
6
7
use Xervice\Core\Business\Model\Config\AbstractConfig;
8
use Xervice\Core\Business\Model\Config\ConfigInterface;
9
use Xervice\Core\Business\Model\Dependency\AbstractDependencyContainer;
10
use Xervice\Core\Business\Model\Dependency\DependencyContainerInterface;
11
use Xervice\Core\Business\Model\Locator\Locator;
12
use Xervice\Core\Business\Model\Locator\Proxy\Business\BusinessLocatorProxy;
13
use Xervice\Core\Business\Model\Locator\Proxy\Business\BusinessLocatorProxyInterface;
14
use Xervice\Core\Business\Model\Locator\Proxy\Communication\CommunicationLocatorProxy;
15
use Xervice\Core\Business\Model\Locator\Proxy\Communication\CommunicationLocatorProxyInterface;
16
use Xervice\Core\Business\Model\Locator\Proxy\Persistence\PersistenceLocatorProxy;
17
use Xervice\Core\Business\Model\Locator\Proxy\Persistence\PersistenceLocatorProxyInterface;
18
use Xervice\Core\Business\Model\Xervice\XerviceInterface;
19
20
abstract class AbstractLocatorProxy implements LocatorProxyInterface
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $coreNamespaces;
26
27
    /**
28
     * @var array
29
     */
30
    protected $projectNamespaces;
31
32
    /**
33
     * @var string
34
     */
35
    protected $serviceName;
36
37
    /**
38
     * @var \Xervice\Core\Business\Model\Config\ConfigInterface
39
     */
40
    protected $config;
41
42
    /**
43
     * @var \Xervice\Core\Business\Model\Dependency\DependencyContainerInterface
44
     */
45
    protected $container;
46
47
    /**
48
     * @var \Xervice\Core\Business\Model\Locator\Proxy\Business\BusinessLocatorProxyInterface
49
     */
50
    protected $businessLocator;
51
52
    /**
53
     * @var \Xervice\Core\Business\Model\Locator\Proxy\Communication\CommunicationLocatorProxyInterface
54
     */
55
    protected $communicationLocator;
56
57
    /**
58
     * @var \Xervice\Core\Business\Model\Locator\Proxy\Persistence\PersistenceLocatorProxyInterface
59
     */
60
    protected $persistenceLocator;
61
62
    /**
63
     * AbstractLocatorProxy constructor.
64
     *
65
     * @param array $coreNamespaces
66
     * @param array $projectNamespaces
67
     * @param string $serviceName
68
     */
69 3
    public function __construct(array $coreNamespaces, array $projectNamespaces, string $serviceName)
70
    {
71 3
        $this->coreNamespaces = $coreNamespaces;
72 3
        $this->projectNamespaces = $projectNamespaces;
73 3
        $this->serviceName = $serviceName;
74 3
        $this->container = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type Xervice\Core\Business\Mo...dencyContainerInterface of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75 3
    }
76
77
    /**
78
     * @return string
79
     */
80 5
    public function name(): string
81
    {
82 5
        return $this->serviceName;
83
    }
84
85
    /**
86
     * @return \Xervice\Core\Business\Model\Locator\Proxy\Business\BusinessLocatorProxy
87
     */
88 8
    public function businessLocator(): BusinessLocatorProxyInterface
89
    {
90 8
        if ($this->businessLocator === null) {
91 3
            $this->businessLocator = new BusinessLocatorProxy(
92 3
                $this->coreNamespaces,
93 3
                $this->projectNamespaces,
94 3
                $this->name()
95
            );
96
        }
97
98 8
        return $this->businessLocator;
99
    }
100
101
    /**
102
     * @return \Xervice\Core\Business\Model\Locator\Proxy\Communication\CommunicationLocatorProxy
103
     */
104 2
    public function communicationLocator(): CommunicationLocatorProxyInterface
105
    {
106 2
        if ($this->communicationLocator === null) {
107 1
            $this->communicationLocator = new CommunicationLocatorProxy(
108 1
                $this->coreNamespaces,
109 1
                $this->projectNamespaces,
110 1
                $this->name()
111
            );
112
        }
113
114 2
        return $this->communicationLocator;
115
    }
116
117
    /**
118
     * @return \Xervice\Core\Business\Model\Locator\Proxy\Persistence\PersistenceLocatorProxy
119
     */
120 3
    public function persistenceLocator(): PersistenceLocatorProxyInterface
121
    {
122 3
        if ($this->communicationLocator === null) {
123 3
            $this->communicationLocator = new PersistenceLocatorProxy(
0 ignored issues
show
Documentation Bug introduced by
It seems like new Xervice\Core\Busines...espaces, $this->name()) of type Xervice\Core\Business\Mo...PersistenceLocatorProxy is incompatible with the declared type Xervice\Core\Business\Mo...onLocatorProxyInterface of property $communicationLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
124 3
                $this->coreNamespaces,
125 3
                $this->projectNamespaces,
126 3
                $this->name()
127
            );
128
        }
129
130 3
        return $this->communicationLocator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->communicationLocator could return the type Xervice\Core\Business\Mo...onLocatorProxyInterface which is incompatible with the type-hinted return Xervice\Core\Business\Mo...ceLocatorProxyInterface. Consider adding an additional type-check to rule them out.
Loading history...
131
    }
132
133
    /**
134
     * @return \Xervice\Core\Business\Model\Config\ConfigInterface
135
     */
136 4
    protected function config(): ConfigInterface
137
    {
138 4
        if ($this->config === null) {
139 4
            $class = $this->getServiceClass('Config') ?: AbstractConfig::class;
140 4
            $this->config = new $class();
141
        }
142
143 4
        return $this->config;
144
    }
145
146
    /**
147
     * @return \Xervice\Core\Business\Model\Dependency\DependencyContainerInterface
148
     */
149 4
    protected function container(): DependencyContainerInterface
150
    {
151 4
        $directory = $this->getDirectory();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $directory is correct as $this->getDirectory() targeting Xervice\Core\Business\Mo...orProxy::getDirectory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
153 4
        if (!isset($this->container[$directory])) {
154 4
            $this->container[$directory] = new AbstractDependencyContainer(
155 4
                $this->config(),
156 4
                Locator::getInstance()
157
            );
158
159 4
            $provider = $this->getServiceClass('DependencyProvider', $directory);
160 4
            if ($provider) {
161 3
                $this->container[$directory] = $this->container[$directory]->register(
162 3
                    new $provider($this->config())
163
                );
164
            }
165
        }
166
167 4
        return $this->container[$directory];
168
    }
169
170
    /**
171
     * @param string $suffix
172
     * @param string|null $directory
173
     *
174
     * @return string|null
175
     */
176 4
    protected function getServiceClass(string $suffix, string $directory = null): ?string
177
    {
178 4
        return $this->getServiceClassFromNamespaces(
179 4
            $this->projectNamespaces,
180 4
            $suffix,
181 4
            $directory
182 4
        ) ?: $this->getServiceClassFromNamespaces(
183 4
            $this->coreNamespaces,
184 4
            $suffix,
185 4
            $directory
186
        );
187
    }
188
189
    /**
190
     * @param array $namespaces
191
     * @param string $suffix
192
     * @param string|null $directory
193
     *
194
     * @return null|string
195
     */
196 4
    protected function getServiceClassFromNamespaces(
197
        array $namespaces,
198
        string $suffix,
199
        string $directory = null
200
    ): ?string {
201 4
        $serviceClass = null;
202
203 4
        foreach ($namespaces as $namespace) {
204 4
            $class = $this->getNamespace($namespace, $suffix, $directory);
205 4
            if (class_exists($class) && is_subclass_of($class, XerviceInterface::class)) {
206 4
                $serviceClass = $class;
207 4
                break;
208
            }
209
        }
210
211 4
        return $serviceClass;
212
    }
213
214
    /**
215
     * @param string $primaryNamespace
216
     * @param string $suffix
217
     * @param string|null $directory
218
     *
219
     * @return string
220
     */
221 4
    protected function getNamespace(string $primaryNamespace, string $suffix, string $directory = null): string
222
    {
223 4
        return sprintf(
224 4
            '\\%1$s\\%2$s%4$s\\%2$s%3$s',
225 4
            $primaryNamespace,
226 4
            $this->name(),
227 4
            $suffix,
228 4
            $directory ? '\\' . $directory : ''
229
        );
230
    }
231
232
    /**
233
     * @return null|string
234
     */
235 3
    protected function getDirectory(): ?string
236
    {
237 3
        return null;
238
    }
239
}