Completed
Push — master ( ce8116...c1c321 )
by Axel
05:10
created

AbstractEntityFactory::setEntityInitialiser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Routes.
5
 *
6
 * @copyright Zikula contributors (Zikula)
7
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
8
 * @author Zikula contributors <[email protected]>.
9
 * @see https://ziku.la
10
 * @version Generated by ModuleStudio 1.4.0 (https://modulestudio.de).
11
 */
12
13
declare(strict_types=1);
14
15
namespace Zikula\RoutesModule\Entity\Factory\Base;
16
17
use Doctrine\ORM\EntityManagerInterface;
18
use Doctrine\ORM\EntityRepository;
19
use InvalidArgumentException;
20
use Zikula\RoutesModule\Entity\Factory\EntityInitialiser;
21
use Zikula\RoutesModule\Entity\RouteEntity;
22
use Zikula\RoutesModule\Helper\CollectionFilterHelper;
23
24
/**
25
 * Factory class used to create entities and receive entity repositories.
26
 */
27
abstract class AbstractEntityFactory
28
{
29
    /**
30
     * @var EntityManagerInterface
31
     */
32
    protected $entityManager;
33
34
    /**
35
     * @var EntityInitialiser
36
     */
37
    protected $entityInitialiser;
38
39
    /**
40
     * @var CollectionFilterHelper
41
     */
42
    protected $collectionFilterHelper;
43
44
    public function __construct(
45
        EntityManagerInterface $entityManager,
46
        EntityInitialiser $entityInitialiser,
47
        CollectionFilterHelper $collectionFilterHelper
48
    ) {
49
        $this->entityManager = $entityManager;
50
        $this->entityInitialiser = $entityInitialiser;
51
        $this->collectionFilterHelper = $collectionFilterHelper;
52
    }
53
54
    /**
55
     * Returns a repository for a given object type.
56
     */
57
    public function getRepository(string $objectType): EntityRepository
58
    {
59
        $entityClass = 'Zikula\\RoutesModule\\Entity\\' . ucfirst($objectType) . 'Entity';
60
61
        /** @var EntityRepository $repository */
62
        $repository = $this->getEntityManager()->getRepository($entityClass);
63
        $repository->setCollectionFilterHelper($this->collectionFilterHelper);
64
65
        return $repository;
66
    }
67
68
    /**
69
     * Creates a new route instance.
70
     */
71
    public function createRoute(): RouteEntity
72
    {
73
        $entity = new RouteEntity();
74
75
        $this->entityInitialiser->initRoute($entity);
76
77
        return $entity;
78
    }
79
80
    /**
81
     * Returns the identifier field's name for a given object type.
82
     */
83
    public function getIdField(string $objectType = ''): string
84
    {
85
        if (empty($objectType)) {
86
            throw new InvalidArgumentException('Invalid object type received.');
87
        }
88
        $entityClass = 'ZikulaRoutesModule:' . ucfirst($objectType) . 'Entity';
89
    
90
        $meta = $this->getEntityManager()->getClassMetadata($entityClass);
91
    
92
        return $meta->getSingleIdentifierFieldName();
0 ignored issues
show
Bug introduced by
The method getSingleIdentifierFieldName() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        return $meta->/** @scrutinizer ignore-call */ getSingleIdentifierFieldName();
Loading history...
93
    }
94
    
95
    public function getEntityManager(): ?EntityManagerInterface
96
    {
97
        return $this->entityManager;
98
    }
99
    
100
    public function getEntityInitialiser(): ?EntityInitialiser
101
    {
102
        return $this->entityInitialiser;
103
    }
104
}
105