Test Failed
Pull Request — master (#31)
by Sébastien
07:15
created

ServiceLocator   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Test Coverage

Coverage 96.61%

Importance

Changes 0
Metric Value
wmc 30
eloc 51
c 0
b 0
f 0
dl 0
loc 258
rs 10
ccs 57
cts 59
cp 0.9661

16 Methods

Rating   Name   Duplication   Size   Complexity  
A repositoryNames() 0 3 1
A setDI() 0 5 1
A setSerializer() 0 9 3
A di() 0 3 1
A registerRepository() 0 3 1
A mappers() 0 3 1
A connections() 0 3 1
A serializer() 0 9 2
A hydrator() 0 7 2
A hydrators() 0 3 1
A __construct() 0 6 4
A repository() 0 17 4
A instantiator() 0 3 1
A connection() 0 3 1
A clearRepositories() 0 9 3
A unregisterRepository() 0 7 3
1
<?php
2
3
namespace Bdf\Prime;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Entity\Hydrator\HydratorInterface;
7
use Bdf\Prime\Entity\Hydrator\HydratorRegistry;
8
use Bdf\Prime\Entity\Instantiator\InstantiatorInterface;
9
use Bdf\Prime\Entity\Instantiator\RegistryInstantiator;
10
use Bdf\Prime\Mapper\MapperFactory;
11
use Bdf\Prime\Repository\EntityRepository;
12
use Bdf\Prime\Repository\RepositoryInterface;
13
use Bdf\Serializer\SerializerInterface;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * ServiceLocator
18
 */
19
class ServiceLocator
20
{
21
    /**
22
     * @var ConnectionManager
23
     */
24
    private $connectionManager;
25
    
26
    /**
27
     * @var RepositoryInterface[]
28
     */
29
    private $repositories = [];
30
    
31
    /**
32
     * @var MapperFactory
33
     */
34
    private $mapperFactory;
35
    
36
    /**
37
     * @var SerializerInterface
38
     */
39
    private $serializer;
40
41
    /**
42
     * @var \Closure
43
     */
44
    private $serializerResolver;
45
46
    /**
47
     * @var HydratorRegistry
48
     */
49
    private $hydrators;
50
51
    /**
52
     * @var InstantiatorInterface
53
     */
54
    private $instantiator;
55
56
    /**
57
     * DI container
58
     *
59
     * @var ContainerInterface
60
     */
61
    private $di;
62
63
    /**
64
     * SericeLocator constructor.
65
     *
66
     * @param ConnectionManager|null $connectionManager
67
     * @param MapperFactory|null  $mapperFactory
68
     * @param InstantiatorInterface|null $instantiator
69
     */
70
    public function __construct(ConnectionManager $connectionManager = null, MapperFactory $mapperFactory = null, InstantiatorInterface $instantiator = null)
71 123
    {
72
        $this->connectionManager = $connectionManager ?: new ConnectionManager();
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
73 123
        $this->mapperFactory = $mapperFactory ?: new MapperFactory();
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
74 123
        $this->instantiator = $instantiator ?: new RegistryInstantiator();
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
75 123
        $this->hydrators = new HydratorRegistry();
76 123
    }
77
78
    /**
79 123
     * Returns connection manager
80 1
     *
81
     * @return ConnectionManager
82 123
     */
83
    public function connections()
84
    {
85 123
        return $this->connectionManager;
86
    }
87
88
    /**
89
     * Returns connection manager
90
     * 
91
     * @return MapperFactory
92 1150
     */
93
    public function mappers()
94 1150
    {
95
        return $this->mapperFactory;
96
    }
97
    
98
    /**
99
     * Get a db connection
100
     * 
101
     * @param string $name
102 130
     * 
103
     * @return ConnectionInterface
104 130
     */
105
    public function connection($name = null)
106
    {
107
        return $this->connectionManager->getConnection($name);
108
    }
109
    
110
    /**
111
     * Register a repository
112
     *
113
     * @param string $entityClass
114 3
     * @param RepositoryInterface $repository
115
     */
116 3
    public function registerRepository($entityClass, RepositoryInterface $repository)
117
    {
118
        $this->repositories[$entityClass] = $repository;
119
    }
120
    
121
    /**
122
     * Unregister a repository
123
     *
124
     * @param string $entityClass
125
     */
126 1049
    public function unregisterRepository($entityClass)
127
    {
128 1049
        if (isset($this->repositories[$entityClass]) && $this->repositories[$entityClass] instanceof EntityRepository) {
129
            $this->repositories[$entityClass]->destroy();
0 ignored issues
show
Bug introduced by
The method destroy() does not exist on Bdf\Prime\Repository\RepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Repository\RepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $this->repositories[$entityClass]->/** @scrutinizer ignore-call */ 
130
                                               destroy();
Loading history...
130
        }
131
132
        unset($this->repositories[$entityClass]);
133
    }
134
    
135
    /**
136
     * Get mapper for specified entity
137 3
     *
138
     * @param string|object $entityClass Name of Entity object to load mapper for
139 3
     * 
140 3
     * @return RepositoryInterface
141
     */
142
    public function repository($entityClass)
143
    {
144
        if (is_object($entityClass)) {
145
            $entityClass = get_class($entityClass);
146
        }
147 1
        
148
        if (!isset($this->repositories[$entityClass])) {
149 1
            $mapper = $this->mapperFactory->build($this, $entityClass);
150
151
            if ($mapper === null) {
152
                return null;
153 1
            }
154 1
155
            $this->repositories[$entityClass] = $mapper->repository();
156
        }
157
        
158
        return $this->repositories[$entityClass];
159
    }
160
    
161
    /**
162
     * Get repository names
163 980
     * 
164
     * @return array
165 980
     */
166 360
    public function repositoryNames()
167
    {
168
        return array_keys($this->repositories);
169 980
    }
170 142
171
    /**
172 142
     * Set the serializer
173 6
     *
174
     * @param \Closure|SerializerInterface $serializer
175
     *
176 136
     * @return $this
177
     */
178
    public function setSerializer($serializer)
179 977
    {
180
        if ($serializer instanceof \Closure) {
181
            $this->serializerResolver = $serializer;
182
        } elseif ($serializer instanceof SerializerInterface) {
0 ignored issues
show
introduced by
$serializer is always a sub-type of Bdf\Serializer\SerializerInterface.
Loading history...
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
183
            $this->serializer = $serializer;
184
        }
185
186
        return $this;
187 3
    }
188
189 3
    /**
190
     * Get the serializer
191
     *
192
     * @return SerializerInterface
193
     */
194
    public function serializer()
195
    {
196
        if ($this->serializerResolver !== null) {
197
            $resolver = $this->serializerResolver;
198
            $this->serializer = $resolver();
199 121
            $this->serializerResolver = null;
200
        }
201 121
202 1
        return $this->serializer;
203 120
    }
204 120
205
    /**
206
     * Get the entity hydrators registry
207 121
     *
208
     * @return HydratorRegistry
209
     */
210
    public function hydrators()
211
    {
212
        return $this->hydrators;
213
    }
214
215 24
    /**
216
     * Get the entity hydrator
217 24
     *
218 1
     * @param string|object $entity The entity class or object
219 1
     *
220 1
     * @return HydratorInterface
221
     */
222
    public function hydrator($entity)
223 24
    {
224
        if (is_object($entity)) {
225
            $entity = get_class($entity);
226
        }
227
228
        return $this->hydrators->get($entity);
229
    }
230
231 3
    /**
232
     * Get the entity instantiator
233 3
     *
234
     * @return InstantiatorInterface
235
     */
236
    public function instantiator()
237
    {
238
        return $this->instantiator;
239
    }
240
241
    /**
242
     * DI accessor
243 592
     *
244
     * @return ContainerInterface
245 592
     */
246 516
    public function di()
247
    {
248
        return $this->di;
249 592
    }
250
251
    /**
252
     * DI accessor
253
     *
254
     * @param ContainerInterface $di
255
     *
256
     * @return $this
257 515
     */
258
    public function setDI(ContainerInterface $di)
0 ignored issues
show
introduced by
Public method name "ServiceLocator::setDI" is not in lowerCamel format
Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
259 515
    {
260
        $this->di = $di;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Clear all cache repositories
267
     */
268
    public function clearRepositories()
269 130
    {
270
        foreach ($this->repositories as $repository) {
271 130
            if ($repository instanceof EntityRepository) {
272
                $repository->destroy();
273
            }
274
        }
275
276
        $this->repositories = [];
277
    }
278
}
279