Passed
Pull Request — master (#31)
by Sébastien
12:54 queued 06:01
created

ServiceLocator::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 120
    public function __construct(ConnectionManager $connectionManager = null, MapperFactory $mapperFactory = null, InstantiatorInterface $instantiator = null)
71
    {
72 120
        $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 120
        $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 120
        $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 120
        $this->hydrators = new HydratorRegistry();
76 120
    }
77
78
    /**
79
     * Returns connection manager
80
     *
81
     * @return ConnectionManager
82
     */
83 1150
    public function connections()
84
    {
85 1150
        return $this->connectionManager;
86
    }
87
88
    /**
89
     * Returns connection manager
90
     * 
91
     * @return MapperFactory
92
     */
93 129
    public function mappers()
94
    {
95 129
        return $this->mapperFactory;
96
    }
97
    
98
    /**
99
     * Get a db connection
100
     * 
101
     * @param string $name
102
     * 
103
     * @return ConnectionInterface
104
     */
105 1050
    public function connection($name = null)
106
    {
107 1050
        return $this->connectionManager->getConnection($name);
108
    }
109
    
110
    /**
111
     * Register a repository
112
     *
113
     * @param string $entityClass
114
     * @param RepositoryInterface $repository
115
     */
116 3
    public function registerRepository($entityClass, RepositoryInterface $repository)
117
    {
118 3
        $this->repositories[$entityClass] = $repository;
119 3
    }
120
    
121
    /**
122
     * Unregister a repository
123
     *
124
     * @param string $entityClass
125
     */
126 1
    public function unregisterRepository($entityClass)
127
    {
128 1
        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 1
        unset($this->repositories[$entityClass]);
133 1
    }
134
    
135
    /**
136
     * Get mapper for specified entity
137
     *
138
     * @param string|object $entityClass Name of Entity object to load mapper for
139
     * 
140
     * @return RepositoryInterface
141
     */
142 980
    public function repository($entityClass)
143
    {
144 980
        if (is_object($entityClass)) {
145 360
            $entityClass = get_class($entityClass);
146
        }
147
        
148 980
        if (!isset($this->repositories[$entityClass])) {
149 142
            $mapper = $this->mapperFactory->build($this, $entityClass);
150
151 142
            if ($mapper === null) {
152 6
                return null;
153
            }
154
155 136
            $this->repositories[$entityClass] = $mapper->repository();
156
        }
157
        
158 977
        return $this->repositories[$entityClass];
159
    }
160
    
161
    /**
162
     * Get repository names
163
     * 
164
     * @return array
165
     */
166 3
    public function repositoryNames()
167
    {
168 3
        return array_keys($this->repositories);
169
    }
170
171
    /**
172
     * Set the serializer
173
     *
174
     * @param \Closure|SerializerInterface $serializer
175
     *
176
     * @return $this
177
     */
178 121
    public function setSerializer($serializer)
179
    {
180 121
        if ($serializer instanceof \Closure) {
181 1
            $this->serializerResolver = $serializer;
182 120
        } 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 120
            $this->serializer = $serializer;
184
        }
185
186 121
        return $this;
187
    }
188
189
    /**
190
     * Get the serializer
191
     *
192
     * @return SerializerInterface
193
     */
194 24
    public function serializer()
195
    {
196 24
        if ($this->serializerResolver !== null) {
197 1
            $resolver = $this->serializerResolver;
198 1
            $this->serializer = $resolver();
199 1
            $this->serializerResolver = null;
200
        }
201
202 24
        return $this->serializer;
203
    }
204
205
    /**
206
     * Get the entity hydrators registry
207
     *
208
     * @return HydratorRegistry
209
     */
210 3
    public function hydrators()
211
    {
212 3
        return $this->hydrators;
213
    }
214
215
    /**
216
     * Get the entity hydrator
217
     *
218
     * @param string|object $entity The entity class or object
219
     *
220
     * @return HydratorInterface
221
     */
222 592
    public function hydrator($entity)
223
    {
224 592
        if (is_object($entity)) {
225 516
            $entity = get_class($entity);
226
        }
227
228 592
        return $this->hydrators->get($entity);
229
    }
230
231
    /**
232
     * Get the entity instantiator
233
     *
234
     * @return InstantiatorInterface
235
     */
236 515
    public function instantiator()
237
    {
238 515
        return $this->instantiator;
239
    }
240
241
    /**
242
     * DI accessor
243
     *
244
     * @return ContainerInterface
245
     */
246 1
    public function di()
247
    {
248 1
        return $this->di;
249
    }
250
251
    /**
252
     * DI accessor
253
     *
254
     * @param ContainerInterface $di
255
     *
256
     * @return $this
257
     */
258 1
    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
    {
260 1
        $this->di = $di;
261
262 1
        return $this;
263
    }
264
265
    /**
266
     * Clear all cache repositories
267
     */
268 1
    public function clearRepositories()
269
    {
270 1
        foreach ($this->repositories as $repository) {
271 1
            if ($repository instanceof EntityRepository) {
272 1
                $repository->destroy();
273
            }
274
        }
275
276 1
        $this->repositories = [];
277 1
    }
278
}
279