Completed
Push — master ( 075a1a...b1f908 )
by max
02:16
created

Config   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 209
Duplicated Lines 14.83 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 2 Features 4
Metric Value
wmc 30
c 10
b 2
f 4
lcom 1
cbo 1
dl 31
loc 209
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTable() 0 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 9 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
    public function getTable($entityName)
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
     * @param string $attribute
33
     * @return string
34
     */
35
    public function getFiled($entityName, $attribute)
36
    {
37 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...
38
            throw new ConfigException(sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName));
39
        }
40
41
        $field = array_search($attribute, $this->entityMap[$entityName]['columnsAsAttributesMap']);
42
43
        if (!$field) {
44
            throw new ConfigException(
45
                sprintf("attributes %s not exists in entity_map[columnsAsAttributesMap] config", $attribute)
46
            );
47
        }
48
49
        return $field;
50
    }
51
52
    /**
53
     * @param string $entityName
54
     * @param string $joinEntityName
55
     *
56
     * @return string
57
     */
58
    public function getRelationExpression($entityName, $joinEntityName)
59
    {
60 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...
61
            throw new ConfigException(
62
                sprintf(
63
                    "entity_map for %s not configured with relation %s",
64
                    $entityName,
65
                    $joinEntityName
66
                )
67
            );
68
        }
69
70
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
71
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])) {
72
            throw new ConfigException(
73
                sprintf(
74
                    "entity_map for %s with relation %s must be array [field, joined-filed]",
75
                    $entityName,
76
                    $joinEntityName
77
                )
78
            );
79
        }
80
81
        return $this->entityMap[$entityName]['relations'][$joinEntityName][0].' = '
82
            . $this->entityMap[$entityName]['relations'][$joinEntityName][1];
83
    }
84
85
    /**
86
     * @param string $entityName
87
     * @param string $joinEntityName
88
     * @return bool
89
     */
90
    public function isRelationManyToMany($entityName, $joinEntityName)
91
    {
92 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...
93
            throw new ConfigException(
94
                sprintf(
95
                    "entity_map for %s not configured with relation %s",
96
                    $entityName,
97
                    $joinEntityName
98
                )
99
            );
100
        }
101
102
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
103
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])
104
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][2])) {
105
            return false;
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * @param string $entityName
113
     * @param string $joinEntityName
114
     * @return array
115
     */
116
    public function getRelationManyToMany($entityName, $joinEntityName)
117
    {
118
        if (!$this->isRelationManyToMany($entityName, $joinEntityName)) {
119
            throw new ConfigException(
120
                sprintf(
121
                    "entity_map for %s with relation %s must be array [link-table, field, joined-filed]",
122
                    $entityName,
123
                    $joinEntityName
124
                )
125
            );
126
        }
127
128
        return [
129
            $this->entityMap[$entityName]['relations'][$joinEntityName][0],
130
            $this->entityMap[$entityName]['relations'][$joinEntityName][1],
131
            $this->entityMap[$entityName]['relations'][$joinEntityName][2],
132
        ];
133
    }
134
135
    /**
136
     * @param string $entityName
137
     *
138
     * @return array
139
     */
140
    public function getColumnsAsAttributesMap($entityName)
141
    {
142 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...
143
            throw new ConfigException(
144
                sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName)
145
            );
146
        }
147
148 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...
149
            throw new ConfigException(
150
                sprintf("entity_map[columnsAsAttributesMap] for %s must be array", $entityName)
151
            );
152
        }
153
154
        return $this->entityMap[$entityName]['columnsAsAttributesMap'];
155
    }
156
157
    /**
158
     * @param string $entityName
159
     * @return string|null
160
     */
161
    public function getPrimaryKey($entityName)
162
    {
163
        if (isset($this->entityMap[$entityName]['primaryKey'])) {
164
            return $this->entityMap[$entityName]['primaryKey'];
165
        }
166
    }
167
168
    /**
169
     * @param string $entityName
170
     * @return string|null
171
     */
172
    public function getSequence($entityName)
173
    {
174
        if (isset($this->entityMap[$entityName]['sequence'])) {
175
            return $this->entityMap[$entityName]['sequence'];
176
        }
177
    }
178
179
    /**
180
     * @param string $entityName
181
     * @return string|null
182
     */
183
    public function getCriteriaMap($entityName)
184
    {
185
        if (isset($this->entityMap[$entityName]['criteriaMap'])) {
186
            return $this->entityMap[$entityName]['criteriaMap'];
187
        }
188
    }
189
190
    /**
191
     * @param string $entityName
192
     * @return string
193
     */
194
    public function getNamespace($entityName)
195
    {
196
        if (isset($this->entityMap[$entityName]['namespace'])) {
197
            return $this->entityMap[$entityName]['namespace'];
198
        }
199
200
        return "{$entityName}s\\$entityName";
201
    }
202
203
    public function getCustomCriteriaClass($entityName, $criteriaName)
204
    {
205
        if (class_exists($criteriaName)) {
206
            return $criteriaName;
207
        }
208
209
        $className = "$entityName\\Infrastructure\\Criteria\\$criteriaName";
210
        return $className;
211
    }
212
}
213