RepositoryFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRepository() 0 10 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