Completed
Push — master ( 8adbc5...dee55e )
by Dmitriy
02:10
created

Config::getSerializedColumns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
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
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
     * @return string
46
     */
47
    public function getCollectionClass($entityName)
48
    {
49
        if (!isset($this->entityMap[$entityName]['collectionClass'])) {
50
            return 'ArrayObject';
51
        }
52
53
        return $this->entityMap[$entityName]['collectionClass'];
54
    }
55
56
    /**
57
     * @param string $entityName
58
     * @param string $attribute
59
     * @return string
60
     */
61
    public function getFiled($entityName, $attribute)
62
    {
63 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...
64
            throw new ConfigException(sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName));
65
        }
66
67
        $field = array_search($attribute, $this->entityMap[$entityName]['columnsAsAttributesMap']);
68
69
        if (!$field) {
70
            throw new ConfigException(
71
                sprintf("attributes %s not exists in entity_map[columnsAsAttributesMap] config", $attribute)
72
            );
73
        }
74
75
        return $field;
76
    }
77
78
    /**
79
     * @param string $entityName
80
     * @param string $joinEntityName
81
     *
82
     * @return string
83
     */
84
    public function getRelationExpression($entityName, $joinEntityName)
85
    {
86 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...
87
            throw new ConfigException(
88
                sprintf(
89
                    "entity_map for %s not configured with relation %s",
90
                    $entityName,
91
                    $joinEntityName
92
                )
93
            );
94
        }
95
96
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
97
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])) {
98
            throw new ConfigException(
99
                sprintf(
100
                    "entity_map for %s with relation %s must be array [field, joined-filed]",
101
                    $entityName,
102
                    $joinEntityName
103
                )
104
            );
105
        }
106
107
        return $this->entityMap[$entityName]['relations'][$joinEntityName][0].' = '
108
            . $this->entityMap[$entityName]['relations'][$joinEntityName][1];
109
    }
110
111
    /**
112
     * @param string $entityName
113
     * @param string $joinEntityName
114
     * @return bool
115
     */
116
    public function isRelationManyToMany($entityName, $joinEntityName)
117
    {
118 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...
119
            throw new ConfigException(
120
                sprintf(
121
                    "entity_map for %s not configured with relation %s",
122
                    $entityName,
123
                    $joinEntityName
124
                )
125
            );
126
        }
127
128
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
129
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])
130
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][2])) {
131
            return false;
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * @param string $entityName
139
     * @param string $joinEntityName
140
     * @return array
141
     */
142
    public function getRelationManyToMany($entityName, $joinEntityName)
143
    {
144
        if (!$this->isRelationManyToMany($entityName, $joinEntityName)) {
145
            throw new ConfigException(
146
                sprintf(
147
                    "entity_map for %s with relation %s must be array [link-table, field, joined-filed]",
148
                    $entityName,
149
                    $joinEntityName
150
                )
151
            );
152
        }
153
154
        return [
155
            $this->entityMap[$entityName]['relations'][$joinEntityName][0],
156
            $this->entityMap[$entityName]['relations'][$joinEntityName][1],
157
            $this->entityMap[$entityName]['relations'][$joinEntityName][2],
158
        ];
159
    }
160
161
    /**
162
     * @param string $entityName
163
     *
164
     * @return array
165
     */
166
    public function getColumnsAsAttributesMap($entityName)
167
    {
168 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...
169
            throw new ConfigException(
170
                sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName)
171
            );
172
        }
173
174 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...
175
            throw new ConfigException(
176
                sprintf("entity_map[columnsAsAttributesMap] for %s must be array", $entityName)
177
            );
178
        }
179
180
        return $this->entityMap[$entityName]['columnsAsAttributesMap'];
181
    }
182
183
    /**
184
     * @param string $entityName
185
     *
186
     * @return array
187
     */
188
    public function getSerializedColumns($entityName)
189
    {
190
        if (!isset($this->entityMap[$entityName]['serializedColumns'])) {
191
            return [];
192
        }
193
194
        if (!is_array($this->entityMap[$entityName]['serializedColumns'])) {
195
            throw new ConfigException(
196
                sprintf("entity_map[serializedColumns] for %s must be array", $entityName)
197
            );
198
        }
199
200
        return $this->entityMap[$entityName]['serializedColumns'];
201
    }
202
203
    /**
204
     * @param string $entityName
205
     * @return string|null
206
     */
207
    public function getPrimaryKey($entityName)
208
    {
209
        if (isset($this->entityMap[$entityName]['primaryKey'])) {
210
            return $this->entityMap[$entityName]['primaryKey'];
211
        }
212
    }
213
214
    /**
215
     * @param string $entityName
216
     * @return string|null
217
     */
218
    public function getSequence($entityName)
219
    {
220
        if (isset($this->entityMap[$entityName]['sequence'])) {
221
            return $this->entityMap[$entityName]['sequence'];
222
        }
223
    }
224
225
    /**
226
     * @param string $entityName
227
     * @return string|null
228
     */
229
    public function getCriteriaMap($entityName)
230
    {
231
        if (isset($this->entityMap[$entityName]['criteriaMap'])) {
232
            return $this->entityMap[$entityName]['criteriaMap'];
233
        }
234
    }
235
236
    /**
237
     * @param string $entityName
238
     * @return string
239
     */
240
    public function getNamespace($entityName)
241
    {
242
        if (isset($this->entityMap[$entityName]['namespace'])) {
243
            return $this->entityMap[$entityName]['namespace'];
244
        }
245
246
        return "{$entityName}s\\$entityName";
247
    }
248
249
    public function getCustomCriteriaClass($entityName, $criteriaName)
250
    {
251
        if (class_exists($criteriaName)) {
252
            return $criteriaName;
253
        }
254
255
        $entityNamespace = $this->getNamespace($entityName);
256
        $className = "$entityNamespace\\Infrastructure\\Criteria\\$criteriaName";
257
        
258
        return $className;
259
    }
260
}
261