Completed
Pull Request — master (#719)
by
unknown
02:16
created

ContainerRepositoryFactory::getRepository()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\Repository;
16
17
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
18
use Doctrine\ORM\EntityManagerInterface;
19
use Doctrine\ORM\Repository\RepositoryFactory;
20
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
21
use Psr\Container\ContainerInterface;
22
23
/**
24
 * Returns repositories that are registered in the container, or a default implementation.
25
 *
26
 * @author Ryan Weaver <[email protected]>
27
 */
28
final class ContainerRepositoryFactory implements RepositoryFactory
29
{
30
    public $container;
31
32
    private $genericRepositories = array();
33
34
    /**
35
     * @var ContainerInterface|SymfonyContainerInterface $container
36
     */
37
    public function __construct(/* ContainerInterface */ $container)
38
    {
39
        /*
40
         * Compatibility layer for Symfony 3.2 and lower.
41
         * When DoctrineBundle requires 3.3 or higher, this can
42
         * be removed and the above type-hint added.
43
         */
44
        if (interface_exists(ContainerInterface::class)) {
45
            if (!$container instanceof ContainerInterface) {
46
                throw new \InvalidArgumentException(sprintf('Argument 1 passed to %s::__construct() must be an instance of "%s"', self::class, ContainerInterface::class));
47
            }
48
        } elseif (!$container instanceof SymfonyContainerInterface) {
49
            throw new \InvalidArgumentException(sprintf('Argument 1 passed to %s::__construct() must be an instance of "%s"', self::class, SymfonyContainerInterface::class));
50
        }
51
52
        $this->container = $container;
53
    }
54
55
    public function getRepository(EntityManagerInterface $entityManager, $entityName)
56
    {
57
        /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
58
        $metadata = $entityManager->getClassMetadata($entityName);
59
        $entityClass = $metadata->name;
60
        $repositoryServiceId = $metadata->customRepositoryClassName;
61
62
        if (null !== $repositoryServiceId) {
63
            if (!$this->container->has($repositoryServiceId)) {
64
                throw new \RuntimeException(sprintf('Could not find the repository service for the "%s" entity. Make sure the "%s" service exists and is tagged with "%s"', $entityName, $repositoryServiceId, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
65
            }
66
67
            return $this->container->get($repositoryServiceId);
68
        }
69
70
        $repositoryHash = $entityClass . spl_object_hash($entityManager);
71
        if (!isset($this->genericRepositories[$repositoryHash])) {
72
            $this->genericRepositories[$repositoryHash] = new DefaultServiceRepository($entityManager, $entityName);
73
        }
74
75
        return $this->genericRepositories[$repositoryHash];
76
    }
77
}
78