Passed
Branch master (7599e2)
by Sébastien
12:18 queued 06:01
created

ServiceLocator::setDI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Prime\Types\TypesRegistryInterface;
14
use Bdf\Serializer\SerializerInterface;
15
use Psr\Container\ContainerInterface;
16
17
/**
18
 * ServiceLocator
19
 */
20
class ServiceLocator
21
{
22
    /**
23
     * @var ConnectionManager
24
     */
25
    private $connectionManager;
26
    
27
    /**
28
     * @var RepositoryInterface[]
29
     */
30
    private $repositories = [];
31
    
32
    /**
33
     * @var MapperFactory
34
     */
35
    private $mapperFactory;
36
    
37
    /**
38
     * @var SerializerInterface
39
     */
40
    private $serializer;
41
42
    /**
43
     * @var \Closure
44
     */
45
    private $serializerResolver;
46
47
    /**
48
     * @var HydratorRegistry
49
     */
50
    private $hydrators;
51
52
    /**
53
     * @var InstantiatorInterface
54
     */
55
    private $instantiator;
56
57
    /**
58
     * DI container
59
     *
60
     * @var ContainerInterface
61
     */
62
    private $di;
63
64
    /**
65
     * SericeLocator constructor.
66
     *
67
     * @param ConnectionManager $connectionManager
68
     * @param MapperFactory  $mapperFactory
69
     * @param InstantiatorInterface $instantiator
70
     */
71 121
    public function __construct(ConnectionManager $connectionManager = null, MapperFactory $mapperFactory = null, InstantiatorInterface $instantiator = null)
72
    {
73 121
        $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...
74 121
        $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...
75 121
        $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...
76 121
        $this->hydrators = new HydratorRegistry();
77 121
    }
78
79
    /**
80
     * Returns connection manager
81
     *
82
     * @return ConnectionManager
83
     */
84 1150
    public function connections()
85
    {
86 1150
        return $this->connectionManager;
87
    }
88
89
    /**
90
     * Returns connection manager
91
     * 
92
     * @return MapperFactory
93
     */
94 129
    public function mappers()
95
    {
96 129
        return $this->mapperFactory;
97
    }
98
    
99
    /**
100
     * Returns connection manager config
101
     * 
102
     * @return Configuration
103
     *
104
     * @deprecated Since 1.1.
0 ignored issues
show
introduced by
The text '@deprecated Since 1.1.' does not match the standard format: @deprecated in deprecation-version and is removed from removal-version. extra-info.
Loading history...
introduced by
Each @deprecated tag must have a @see tag immediately following it.
Loading history...
105
     */
106 2
    public function config()
107
    {
108 2
        return $this->connectionManager->config();
0 ignored issues
show
Deprecated Code introduced by
The function Bdf\Prime\ConnectionManager::config() has been deprecated: Every connection should have its config. ( Ignorable by Annotation )

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

108
        return /** @scrutinizer ignore-deprecated */ $this->connectionManager->config();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
    }
110
    
111
    /**
112
     * Get a db connection
113
     * 
114
     * @param string $name
115
     * 
116
     * @return ConnectionInterface
117
     */
118 1049
    public function connection($name = null)
119
    {
120 1049
        return $this->connectionManager->getConnection($name);
121
    }
122
    
123
    /**
124
     * Register a repository
125
     *
126
     * @param string $entityClass
127
     * @param RepositoryInterface $repository
128
     */
129 3
    public function registerRepository($entityClass, RepositoryInterface $repository)
130
    {
131 3
        $this->repositories[$entityClass] = $repository;
132 3
    }
133
    
134
    /**
135
     * Unregister a repository
136
     *
137
     * @param string $entityClass
138
     */
139 1
    public function unregisterRepository($entityClass)
140
    {
141 1
        if (isset($this->repositories[$entityClass]) && $this->repositories[$entityClass] instanceof EntityRepository) {
142
            $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

142
            $this->repositories[$entityClass]->/** @scrutinizer ignore-call */ 
143
                                               destroy();
Loading history...
143
        }
144
145 1
        unset($this->repositories[$entityClass]);
146 1
    }
147
    
148
    /**
149
     * Get mapper for specified entity
150
     *
151
     * @param string|object $entityClass Name of Entity object to load mapper for
152
     * 
153
     * @return RepositoryInterface
154
     */
155 980
    public function repository($entityClass)
156
    {
157 980
        if (is_object($entityClass)) {
158 360
            $entityClass = get_class($entityClass);
159
        }
160
        
161 980
        if (!isset($this->repositories[$entityClass])) {
162 142
            $mapper = $this->mapperFactory->build($this, $entityClass);
163
164 142
            if ($mapper === null) {
165 6
                return null;
166
            }
167
168 136
            $this->repositories[$entityClass] = $mapper->repository();
169
        }
170
        
171 977
        return $this->repositories[$entityClass];
172
    }
173
    
174
    /**
175
     * Get repository names
176
     * 
177
     * @return array
178
     */
179 3
    public function repositoryNames()
180
    {
181 3
        return array_keys($this->repositories);
182
    }
183
184
    /**
185
     * Set the serializer
186
     *
187
     * @param \Closure|SerializerInterface $serializer
188
     *
189
     * @return $this
190
     */
191 121
    public function setSerializer($serializer)
192
    {
193 121
        if ($serializer instanceof \Closure) {
194 1
            $this->serializerResolver = $serializer;
195 120
        } elseif ($serializer instanceof SerializerInterface) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
introduced by
$serializer is always a sub-type of Bdf\Serializer\SerializerInterface.
Loading history...
196 120
            $this->serializer = $serializer;
197
        }
198
199 121
        return $this;
200
    }
201
202
    /**
203
     * Get the serializer
204
     *
205
     * @return SerializerInterface
206
     */
207 24
    public function serializer()
208
    {
209 24
        if ($this->serializerResolver !== null) {
210 1
            $resolver = $this->serializerResolver;
211 1
            $this->serializer = $resolver();
212 1
            $this->serializerResolver = null;
213
        }
214
215 24
        return $this->serializer;
216
    }
217
218
    /**
219
     * Get the entity hydrators registry
220
     *
221
     * @return HydratorRegistry
222
     */
223 3
    public function hydrators()
224
    {
225 3
        return $this->hydrators;
226
    }
227
228
    /**
229
     * Get the entity hydrator
230
     *
231
     * @param string|object $entity The entity class or object
232
     *
233
     * @return HydratorInterface
234
     */
235 592
    public function hydrator($entity)
236
    {
237 592
        if (is_object($entity)) {
238 516
            $entity = get_class($entity);
239
        }
240
241 592
        return $this->hydrators->get($entity);
242
    }
243
244
    /**
245
     * Get the entity instantiator
246
     *
247
     * @return InstantiatorInterface
248
     */
249 515
    public function instantiator()
250
    {
251 515
        return $this->instantiator;
252
    }
253
254
    /**
255
     * Get the types registry
256
     *
257
     * @return TypesRegistryInterface
258
     *
259
     * @deprecated Since 1.1.
0 ignored issues
show
introduced by
The text '@deprecated Since 1.1.' does not match the standard format: @deprecated in deprecation-version and is removed from removal-version. extra-info.
Loading history...
introduced by
Each @deprecated tag must have a @see tag immediately following it.
Loading history...
260
     */
261 130
    public function types()
262
    {
263 130
        return $this->connectionManager->config()->getTypes();
0 ignored issues
show
Deprecated Code introduced by
The function Bdf\Prime\ConnectionManager::config() has been deprecated: Every connection should have its config. ( Ignorable by Annotation )

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

263
        return /** @scrutinizer ignore-deprecated */ $this->connectionManager->config()->getTypes();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
264
    }
265
266
    /**
267
     * DI accessor
268
     *
269
     * @return ContainerInterface
270
     */
271 1
    public function di()
272
    {
273 1
        return $this->di;
274
    }
275
276
    /**
277
     * DI accessor
278
     *
279
     * @param ContainerInterface $di
280
     *
281
     * @return $this
282
     */
283 1
    public function setDI(ContainerInterface $di)
0 ignored issues
show
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...
introduced by
Public method name "ServiceLocator::setDI" is not in lowerCamel format
Loading history...
284
    {
285 1
        $this->di = $di;
286
287 1
        return $this;
288
    }
289
290
    /**
291
     * Clear all cache repositories
292
     */
293 1
    public function clearRepositories()
294
    {
295 1
        foreach ($this->repositories as $repository) {
296 1
            if ($repository instanceof EntityRepository) {
297 1
                $repository->destroy();
298
            }
299
        }
300
301 1
        $this->repositories = [];
302 1
    }
303
}
304