Completed
Push — master ( 7a6bdd...324ce1 )
by Sébastien
09:34
created

ServiceLocator::hydrators()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Prime;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
6
use Bdf\Prime\Entity\Hydrator\HydratorInterface;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
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;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
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 106
    public function __construct(ConnectionManager $connectionManager = null, MapperFactory $mapperFactory = null, InstantiatorInterface $instantiator = null)
72
    {
73 106
        $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 IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
74 106
        $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 IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
75 106
        $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 IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
76 106
        $this->hydrators = new HydratorRegistry();
77
78
        // TODO Legacy. Should be removed
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
79 106
        if ($cache = $this->connectionManager->config()->getMetadataCache()) {
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

79
        if ($cache = /** @scrutinizer ignore-deprecated */ $this->connectionManager->config()->getMetadataCache()) {

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...
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
80 1
            $this->mapperFactory->setMetadataCache($cache);
81
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
82 106
        if ($cache = $this->connectionManager->config()->getResultCache()) {
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

82
        if ($cache = /** @scrutinizer ignore-deprecated */ $this->connectionManager->config()->getResultCache()) {

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...
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
83
            $this->mapperFactory->setResultCache($cache);
84
        }
85 106
    }
86
87
    /**
88
     * Returns connection manager
89
     *
90
     * @return ConnectionManager
91
     */
92 1105
    public function connections()
93
    {
94 1105
        return $this->connectionManager;
95
    }
96
97
    /**
98
     * Returns connection manager
99
     * 
100
     * @return MapperFactory
101
     */
102 131
    public function mappers()
103
    {
104 131
        return $this->mapperFactory;
105
    }
106
    
107
    /**
108
     * Returns connection manager config
109
     * 
110
     * @return Configuration
111
     *
112
     * @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...
113
     */
114 3
    public function config()
115
    {
116 3
        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

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

150
            $this->repositories[$entityClass]->/** @scrutinizer ignore-call */ 
151
                                               destroy();
Loading history...
151
        }
152
153 1
        unset($this->repositories[$entityClass]);
154 1
    }
155
    
156
    /**
157
     * Get mapper for specified entity
158
     *
159
     * @param string|object $entityClass Name of Entity object to load mapper for
160
     * 
161
     * @return RepositoryInterface
162
     */
163 934
    public function repository($entityClass)
164
    {
165 934
        if (is_object($entityClass)) {
166 353
            $entityClass = get_class($entityClass);
167
        }
168
        
169 934
        if (!isset($this->repositories[$entityClass])) {
170 122
            $mapper = $this->mapperFactory->build($this, $entityClass);
171
172 122
            if ($mapper === null) {
173 6
                return null;
174
            }
175
176 116
            $this->repositories[$entityClass] = $mapper->repository();
177
        }
178
        
179 931
        return $this->repositories[$entityClass];
180
    }
181
    
182
    /**
183
     * Get repository names
184
     * 
185
     * @return array
186
     */
187 3
    public function repositoryNames()
188
    {
189 3
        return array_keys($this->repositories);
190
    }
191
192
    /**
193
     * Set the serializer
194
     *
195
     * @param \Closure|SerializerInterface $serializer
196
     *
197
     * @return $this
198
     */
199 104
    public function setSerializer($serializer)
200
    {
201 104
        if ($serializer instanceof \Closure) {
202 1
            $this->serializerResolver = $serializer;
203 103
        } 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...
204 103
            $this->serializer = $serializer;
205
        }
206
207 104
        return $this;
208
    }
209
210
    /**
211
     * Get the serializer
212
     *
213
     * @return SerializerInterface
214
     */
215 24
    public function serializer()
216
    {
217 24
        if ($this->serializerResolver !== null) {
218 1
            $resolver = $this->serializerResolver;
219 1
            $this->serializer = $resolver();
220 1
            $this->serializerResolver = null;
221
        }
222
223 24
        return $this->serializer;
224
    }
225
226
    /**
227
     * Get the entity hydrators registry
228
     *
229
     * @return HydratorRegistry
230
     */
231 3
    public function hydrators()
232
    {
233 3
        return $this->hydrators;
234
    }
235
236
    /**
237
     * Get the entity hydrator
238
     *
239
     * @param string|object $entity The entity class or object
240
     *
241
     * @return HydratorInterface
242
     */
243 565
    public function hydrator($entity)
244
    {
245 565
        if (is_object($entity)) {
246 497
            $entity = get_class($entity);
247
        }
248
249 565
        return $this->hydrators->get($entity);
250
    }
251
252
    /**
253
     * Get the entity instantiator
254
     *
255
     * @return InstantiatorInterface
256
     */
257 493
    public function instantiator()
258
    {
259 493
        return $this->instantiator;
260
    }
261
262
    /**
263
     * Get the types registry
264
     *
265
     * @return TypesRegistryInterface
266
     *
267
     * @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...
268
     */
269 110
    public function types()
270
    {
271 110
        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

271
        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...
272
    }
273
274
    /**
275
     * DI accessor
276
     *
277
     * @return ContainerInterface
278
     */
279 1
    public function di()
280
    {
281 1
        return $this->di;
282
    }
283
284
    /**
285
     * DI accessor
286
     *
287
     * @param ContainerInterface $di
288
     *
289
     * @return $this
290
     */
291 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...
292
    {
293 1
        $this->di = $di;
294
295 1
        return $this;
296
    }
297
298
    /**
299
     * Clear all cache repositories
300
     */
301 1
    public function clearRepositories()
302
    {
303 1
        foreach ($this->repositories as $repository) {
304 1
            if ($repository instanceof EntityRepository) {
305 1
                $repository->destroy();
306
            }
307
        }
308
309 1
        $this->repositories = [];
310 1
    }
311
}
312