RepositoryFactory::getRepository()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lendable\DoctrineExtensionsBundle\Doctrine\Repository;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Repository\RepositoryFactory as RepositoryFactoryInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class RepositoryFactory implements RepositoryFactoryInterface
12
{
13
    /**
14
     * Container to retrieve repositories from.
15
     *
16
     * @var ContainerInterface
17
     */
18
    private $container;
19
20
    /**
21
     * Map of fully qualified entity class names => service IDs.
22
     *
23
     * @var string[]
24
     */
25
    private $fqcnToServiceIdMap;
26
27
    /**
28
     * The repository factory to fallback to if the entity is not configured
29
     * as a service.
30
     *
31
     * @var RepositoryFactoryInterface
32
     */
33
    private $fallback;
34
35
    public function __construct(ContainerInterface $container, array $fqcnToServiceIdMap, RepositoryFactoryInterface $fallback)
36
    {
37
        $this->container = $container;
38
        $this->fqcnToServiceIdMap = $fqcnToServiceIdMap;
39
        $this->fallback = $fallback;
40
    }
41
42
    public function getRepository(EntityManagerInterface $entityManager, $entityName)
43
    {
44
        $className = $entityManager->getClassMetadata($entityName)->getName();
45
46
        if (isset($this->fqcnToServiceIdMap[$className])) {
47
            return $this->container->get($this->fqcnToServiceIdMap[$className]);
48
        }
49
50
        return $this->fallback->getRepository($entityManager, $entityName);
51
    }
52
}
53