Passed
Push — master ( 547c02...88948f )
by Siim
10:33
created

RepositoryFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 16.02.19
6
 * Time: 19:28
7
 */
8
9
namespace Sf4\Api\Repository;
10
11
use Doctrine\ORM\EntityManagerInterface;
12
13
class RepositoryFactory
14
{
15
16
    /** @var EntityManagerInterface $entityManager */
17
    protected $entityManager;
18
19
    /** @var array $entities */
20
    protected $entities;
21
22
    /**
23
     * RepositoryFactory constructor.
24
     * @param EntityManagerInterface $entityManager
25
     * @param array $entities
26
     */
27
    public function __construct(EntityManagerInterface $entityManager, array $entities)
28
    {
29
        $this->entityManager = $entityManager;
30
        $this->entities = $entities;
31
    }
32
33
    /**
34
     * @param string $tableName
35
     * @return RepositoryInterface|null
36
     */
37
    public function create(string $tableName): ?RepositoryInterface
38
    {
39
        if (isset($this->entities[$tableName])) {
40
            $entityClass = $this->entities[$tableName];
41
            $repository = $this->entityManager->getRepository($entityClass);
42
43
            if ($repository instanceof RepositoryInterface) {
44
                return $repository;
45
            }
46
        }
47
48
        return null;
49
    }
50
}
51