Completed
Pull Request — master (#274)
by
unknown
17:41 queued 14:28
created

EntityLocator::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 * Copyright (c) 2016 Jaroslav Hranička ([email protected])
8
 *
9
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
10
 */
11
12
namespace Kdyby\Doctrine\DI;
13
14
use Doctrine;
15
use Kdyby;
16
use Nette;
17
use Nette\DI\Container;
18
19
class EntityLocator
20
{
21
22
	const CACHE_NS = 'Doctrine.EntityLocator';
23
24
	/** @var Nette\Caching\Cache */
25
	private $cache;
26
27
	/** @var array */
28
	private $map = [];
29
30
	public function __construct(Nette\Caching\IStorage $storage)
31
	{
32
		$this->cache = new Nette\Caching\Cache($storage, self::CACHE_NS);
33
	}
34
35
	public function setup(Container $container, $containerFile, array $queue, array $classNames, array $managers)
36
	{
37
		if (empty($queue)) {
38
			return;
39
		}
40
41
		$this->map = $this->cache->load($containerFile);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cache->load($containerFile) of type * is incompatible with the declared type array of property $map.

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...
42
43
		if ($this->map === NULL) {
44
			foreach ($queue as $manager => $items) {
45
				$repository = $classNames[$manager];
46
				/** @var Kdyby\Doctrine\EntityManager $entityManager */
47
				$entityManager = $container->getService($managers[$manager]);
48
				/** @var Doctrine\ORM\Mapping\ClassMetadata $entityMetadata */
49
				$metadataFactory = $entityManager->getMetadataFactory();
50
51
				$allMetadata = [];
52
				foreach ($metadataFactory->getAllMetadata() as $entityMetadata) {
53
					if ($repository === $entityMetadata->customRepositoryClassName || empty($entityMetadata->customRepositoryClassName)) {
54
						continue;
55
					}
56
57
					$allMetadata[ltrim($entityMetadata->customRepositoryClassName, '\\')] = $entityMetadata;
58
				}
59
60
				foreach ($items as $item) {
61
					if (!isset($allMetadata[$item[0]])) {
62
						throw new Nette\Utils\AssertionException(sprintf('Repository class %s have been found in DIC, but no entity has it assigned and it has no entity configured', $item[0]));
63
					}
64
65
					$entityMetadata = $allMetadata[$item[0]];
66
					$this->map[$item[0]] = $entityMetadata->getName();
67
				}
68
			}
69
70
			$this->cache->save($containerFile, $this->map, [
71
				Nette\Caching\Cache::FILES => [$containerFile],
72
			]);
73
		}
74
	}
75
76
	public function get($repositoryName)
77
	{
78
		if (isset($this->map[$repositoryName])) {
79
			return $this->map[$repositoryName];
80
		} else {
81
			throw new Kdyby\Doctrine\InvalidArgumentException('Entity for repository %s was not found.', $repositoryName);
82
		}
83
	}
84
85
}
86