Completed
Push — master ( e715c7...ffa9af )
by Craig
08:48 queued 02:34
created

AbstractRoutesFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 16.5 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 17
loc 103
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRepository() 0 6 1
A createRoute() 0 6 1
A getIdFields() 17 17 3
A hasCompositeKeys() 0 4 1
A getObjectManager() 0 4 1
A setObjectManager() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
18
/**
19
 * Factory class used to create entities and receive entity repositories.
20
 *
21
 * This is the base factory class.
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
     * RoutesFactory constructor.
32
     *
33
     * @param ObjectManager $objectManager The object manager to be used for determining the repositories
34
     */
35
    public function __construct(ObjectManager $objectManager)
36
    {
37
        $this->objectManager = $objectManager;
38
    }
39
40
    /**
41
     * Returns a repository for a given object type.
42
     *
43
     * @param string $objectType Name of desired entity type
44
     *
45
     * @return EntityRepository The repository responsible for the given object type
46
     */
47
    public function getRepository($objectType)
48
    {
49
        $entityClass = 'Zikula\\RoutesModule\\Entity\\' . ucfirst($objectType) . 'Entity';
50
51
        return $this->objectManager->getRepository($entityClass);
52
    }
53
54
    /**
55
     * Creates a new route instance.
56
     *
57
     * @return Zikula\RoutesModule\Entity\routeEntity The newly created entity instance
58
     */
59
    public function createRoute()
60
    {
61
        $entityClass = 'Zikula\\RoutesModule\\Entity\\RouteEntity';
62
63
        return new $entityClass();
64
    }
65
66
    /**
67
     * Gets the list of identifier fields for a given object type.
68
     *
69
     * @param string $objectType The object type to be treated
70
     *
71
     * @return array List of identifier field names
72
     */
73 View Code Duplication
    public function getIdFields($objectType = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        if (empty($objectType)) {
76
            throw new InvalidArgumentException('Invalid object type received.');
77
        }
78
        $entityClass = 'ZikulaRoutesModule:' . ucfirst($objectType) . 'Entity';
79
    
80
        $meta = $this->entityFactory->getObjectManager()->getClassMetadata($entityClass);
0 ignored issues
show
Bug introduced by
The property entityFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
    
82
        if ($this->hasCompositeKeys($objectType)) {
83
            $idFields = $meta->getIdentifierFieldNames();
84
        } else {
85
            $idFields = [$meta->getSingleIdentifierFieldName()];
86
        }
87
    
88
        return $idFields;
89
    }
90
91
    /**
92
     * Checks whether a certain entity type uses composite keys or not.
93
     *
94
     * @param string $objectType The object type to retrieve
95
     *
96
     * @return Boolean Whether composite keys are used or not
97
     */
98
    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...
99
    {
100
        return false;
101
    }
102
103
    /**
104
     * Returns the object manager.
105
     *
106
     * @return ObjectManager
107
     */
108
    public function getObjectManager()
109
    {
110
        return $this->objectManager;
111
    }
112
    
113
    /**
114
     * Sets the object manager.
115
     *
116
     * @param ObjectManager $objectManager
117
     *
118
     * @return void
119
     */
120
    public function setObjectManager($objectManager)
121
    {
122
        $this->objectManager = $objectManager;
123
    }
124
    
125
}
126