Completed
Push — master ( b4dd2a...679ce7 )
by Craig
11:55
created

AbstractRoutesFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRepository() 0 6 1
A createRoute() 0 10 1
A getIdFields() 0 17 3
A hasCompositeKeys() 0 4 1
A getObjectManager() 0 4 1
A setObjectManager() 0 6 2
A getEntityInitialiser() 0 4 1
A setEntityInitialiser() 0 6 2
1
<?php
2
/**
3
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.4 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Entity\Factory\Base;
14
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\EntityRepository;
17
use InvalidArgumentException;
18
use Zikula\RoutesModule\Entity\Factory\EntityInitialiser;
19
20
/**
21
 * Factory class used to create entities and receive entity repositories.
22
 */
23
abstract class AbstractRoutesFactory
24
{
25
    /**
26
     * @var ObjectManager The object manager to be used for determining the repository
27
     */
28
    protected $objectManager;
29
30
    /**
31
     * @var EntityInitialiser The entity initialiser for dynamical application of default values
32
     */
33
    protected $entityInitialiser;
34
35
    /**
36
     * RoutesFactory constructor.
37
     *
38
     * @param ObjectManager     $objectManager     The object manager to be used for determining the repositories
39
     * @param EntityInitialiser $entityInitialiser The entity initialiser for dynamical application of default values
40
     */
41
    public function __construct(ObjectManager $objectManager, EntityInitialiser $entityInitialiser)
42
    {
43
        $this->objectManager = $objectManager;
44
        $this->entityInitialiser = $entityInitialiser;
45
    }
46
47
    /**
48
     * Returns a repository for a given object type.
49
     *
50
     * @param string $objectType Name of desired entity type
51
     *
52
     * @return EntityRepository The repository responsible for the given object type
53
     */
54
    public function getRepository($objectType)
55
    {
56
        $entityClass = 'Zikula\\RoutesModule\\Entity\\' . ucfirst($objectType) . 'Entity';
57
58
        return $this->objectManager->getRepository($entityClass);
59
    }
60
61
    /**
62
     * Creates a new route instance.
63
     *
64
     * @return Zikula\RoutesModule\Entity\routeEntity The newly created entity instance
65
     */
66
    public function createRoute()
67
    {
68
        $entityClass = 'Zikula\\RoutesModule\\Entity\\RouteEntity';
69
70
        $entity = new $entityClass();
71
72
        $this->entityInitialiser->initRoute($entity);
73
74
        return $entity;
75
    }
76
77
    /**
78
     * Gets the list of identifier fields for a given object type.
79
     *
80
     * @param string $objectType The object type to be treated
81
     *
82
     * @return array List of identifier field names
83
     */
84
    public function getIdFields($objectType = '')
85
    {
86
        if (empty($objectType)) {
87
            throw new InvalidArgumentException('Invalid object type received.');
88
        }
89
        $entityClass = 'ZikulaRoutesModule:' . ucfirst($objectType) . 'Entity';
90
    
91
        $meta = $this->getObjectManager()->getClassMetadata($entityClass);
92
    
93
        if ($this->hasCompositeKeys($objectType)) {
94
            $idFields = $meta->getIdentifierFieldNames();
95
        } else {
96
            $idFields = [$meta->getSingleIdentifierFieldName()];
97
        }
98
    
99
        return $idFields;
100
    }
101
102
    /**
103
     * Checks whether a certain entity type uses composite keys or not.
104
     *
105
     * @param string $objectType The object type to retrieve
106
     *
107
     * @return Boolean Whether composite keys are used or not
108
     */
109
    public function hasCompositeKeys($objectType)
0 ignored issues
show
Unused Code introduced by
The parameter $objectType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return false;
112
    }
113
114
    /**
115
     * Returns the object manager.
116
     *
117
     * @return ObjectManager
118
     */
119
    public function getObjectManager()
120
    {
121
        return $this->objectManager;
122
    }
123
    
124
    /**
125
     * Sets the object manager.
126
     *
127
     * @param ObjectManager $objectManager
128
     *
129
     * @return void
130
     */
131
    public function setObjectManager($objectManager)
132
    {
133
        if ($this->objectManager != $objectManager) {
134
            $this->objectManager = $objectManager;
135
        }
136
    }
137
    
138
139
    /**
140
     * Returns the entity initialiser.
141
     *
142
     * @return EntityInitialiser
143
     */
144
    public function getEntityInitialiser()
145
    {
146
        return $this->entityInitialiser;
147
    }
148
    
149
    /**
150
     * Sets the entity initialiser.
151
     *
152
     * @param EntityInitialiser $entityInitialiser
153
     *
154
     * @return void
155
     */
156
    public function setEntityInitialiser($entityInitialiser)
157
    {
158
        if ($this->entityInitialiser != $entityInitialiser) {
159
            $this->entityInitialiser = $entityInitialiser;
160
        }
161
    }
162
    
163
}
164