Completed
Push — master ( bfdf5c...ea07f3 )
by Siim
13:49
created

RepositoryFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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
        $entityClass = $this->getEntityClass($tableName);
40
        if ($entityClass) {
41
            $repository = $this->entityManager->getRepository($entityClass);
42
            if ($repository instanceof RepositoryInterface) {
43
                return $repository;
44
            }
45
        }
46
47
        return null;
48
    }
49
50
    /**
51
     * @param string $tableName
52
     * @return string|null
53
     */
54
    public function getEntityClass(string $tableName): ?string
55
    {
56
        if (isset($this->entities[$tableName])) {
57
            return $this->entities[$tableName];
58
        }
59
60
        return null;
61
    }
62
}
63