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

RepositoryFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEntityClass() 0 7 2
A create() 0 11 3
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