Completed
Push — master ( 9267ff...2d0f09 )
by max
02:39
created

Config   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 224
Duplicated Lines 20.98 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 12
Bugs 3 Features 4
Metric Value
wmc 32
c 12
b 3
f 4
lcom 1
cbo 1
dl 47
loc 224
rs 9.6

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTable() 8 8 2
A getEntityClass() 8 8 2
A getFiled() 3 16 3
B getRelationExpression() 9 26 4
B isRelationManyToMany() 9 20 5
A getRelationManyToMany() 0 18 2
A getColumnsAsAttributesMap() 10 16 3
A getPrimaryKey() 0 6 2
A getSequence() 0 6 2
A getCriteriaMap() 0 6 2
A getNamespace() 0 8 2
A getCustomCriteriaClass() 0 11 2

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
namespace T4webInfrastructure;
3
4
class Config
5
{
6
    /**
7
     * @var array
8
     */
9
    protected $entityMap = [];
10
11
    public function __construct(array $entityMap)
12
    {
13
        $this->entityMap = $entityMap;
14
    }
15
16
    /**
17
     * @param string $entityName
18
     *
19
     * @return string
20
     */
21 View Code Duplication
    public function getTable($entityName)
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...
22
    {
23
        if (!isset($this->entityMap[$entityName]['table'])) {
24
            throw new ConfigException(sprintf("entity_map not configured for %s", $entityName));
25
        }
26
27
        return $this->entityMap[$entityName]['table'];
28
    }
29
30
    /**
31
     * @param string $entityName
32
     * @return string mixed
33
     */
34 View Code Duplication
    public function getEntityClass($entityName)
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...
35
    {
36
        if (!isset($this->entityMap[$entityName]['entityClass'])) {
37
            throw new ConfigException(sprintf("entity_map[entityClass] not configured for %s", $entityName));
38
        }
39
40
        return $this->entityMap[$entityName]['entityClass'];
41
    }
42
43
    /**
44
     * @param string $entityName
45
     * @param string $attribute
46
     * @return string
47
     */
48
    public function getFiled($entityName, $attribute)
49
    {
50 View Code Duplication
        if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            throw new ConfigException(sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName));
52
        }
53
54
        $field = array_search($attribute, $this->entityMap[$entityName]['columnsAsAttributesMap']);
55
56
        if (!$field) {
57
            throw new ConfigException(
58
                sprintf("attributes %s not exists in entity_map[columnsAsAttributesMap] config", $attribute)
59
            );
60
        }
61
62
        return $field;
63
    }
64
65
    /**
66
     * @param string $entityName
67
     * @param string $joinEntityName
68
     *
69
     * @return string
70
     */
71
    public function getRelationExpression($entityName, $joinEntityName)
72
    {
73 View Code Duplication
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            throw new ConfigException(
75
                sprintf(
76
                    "entity_map for %s not configured with relation %s",
77
                    $entityName,
78
                    $joinEntityName
79
                )
80
            );
81
        }
82
83
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
84
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])) {
85
            throw new ConfigException(
86
                sprintf(
87
                    "entity_map for %s with relation %s must be array [field, joined-filed]",
88
                    $entityName,
89
                    $joinEntityName
90
                )
91
            );
92
        }
93
94
        return $this->entityMap[$entityName]['relations'][$joinEntityName][0].' = '
95
            . $this->entityMap[$entityName]['relations'][$joinEntityName][1];
96
    }
97
98
    /**
99
     * @param string $entityName
100
     * @param string $joinEntityName
101
     * @return bool
102
     */
103
    public function isRelationManyToMany($entityName, $joinEntityName)
104
    {
105 View Code Duplication
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
106
            throw new ConfigException(
107
                sprintf(
108
                    "entity_map for %s not configured with relation %s",
109
                    $entityName,
110
                    $joinEntityName
111
                )
112
            );
113
        }
114
115
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
116
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])
117
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][2])) {
118
            return false;
119
        }
120
121
        return true;
122
    }
123
124
    /**
125
     * @param string $entityName
126
     * @param string $joinEntityName
127
     * @return array
128
     */
129
    public function getRelationManyToMany($entityName, $joinEntityName)
130
    {
131
        if (!$this->isRelationManyToMany($entityName, $joinEntityName)) {
132
            throw new ConfigException(
133
                sprintf(
134
                    "entity_map for %s with relation %s must be array [link-table, field, joined-filed]",
135
                    $entityName,
136
                    $joinEntityName
137
                )
138
            );
139
        }
140
141
        return [
142
            $this->entityMap[$entityName]['relations'][$joinEntityName][0],
143
            $this->entityMap[$entityName]['relations'][$joinEntityName][1],
144
            $this->entityMap[$entityName]['relations'][$joinEntityName][2],
145
        ];
146
    }
147
148
    /**
149
     * @param string $entityName
150
     *
151
     * @return array
152
     */
153
    public function getColumnsAsAttributesMap($entityName)
154
    {
155 View Code Duplication
        if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
156
            throw new ConfigException(
157
                sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName)
158
            );
159
        }
160
161 View Code Duplication
        if (!is_array($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
162
            throw new ConfigException(
163
                sprintf("entity_map[columnsAsAttributesMap] for %s must be array", $entityName)
164
            );
165
        }
166
167
        return $this->entityMap[$entityName]['columnsAsAttributesMap'];
168
    }
169
170
    /**
171
     * @param string $entityName
172
     * @return string|null
173
     */
174
    public function getPrimaryKey($entityName)
175
    {
176
        if (isset($this->entityMap[$entityName]['primaryKey'])) {
177
            return $this->entityMap[$entityName]['primaryKey'];
178
        }
179
    }
180
181
    /**
182
     * @param string $entityName
183
     * @return string|null
184
     */
185
    public function getSequence($entityName)
186
    {
187
        if (isset($this->entityMap[$entityName]['sequence'])) {
188
            return $this->entityMap[$entityName]['sequence'];
189
        }
190
    }
191
192
    /**
193
     * @param string $entityName
194
     * @return string|null
195
     */
196
    public function getCriteriaMap($entityName)
197
    {
198
        if (isset($this->entityMap[$entityName]['criteriaMap'])) {
199
            return $this->entityMap[$entityName]['criteriaMap'];
200
        }
201
    }
202
203
    /**
204
     * @param string $entityName
205
     * @return string
206
     */
207
    public function getNamespace($entityName)
208
    {
209
        if (isset($this->entityMap[$entityName]['namespace'])) {
210
            return $this->entityMap[$entityName]['namespace'];
211
        }
212
213
        return "{$entityName}s\\$entityName";
214
    }
215
216
    public function getCustomCriteriaClass($entityName, $criteriaName)
217
    {
218
        if (class_exists($criteriaName)) {
219
            return $criteriaName;
220
        }
221
222
        $entityNamespace = $this->getNamespace($entityName);
223
        $className = "$entityNamespace\\Infrastructure\\Criteria\\$criteriaName";
224
        
225
        return $className;
226
    }
227
}
228