Completed
Push — master ( 3eeaf5...0122c0 )
by Julien
05:59 queued 03:17
created

ClassMetadata   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Test Coverage

Coverage 37.29%

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 293
rs 9.84
c 0
b 0
f 0
ccs 22
cts 59
cp 0.3729
wmc 32

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelName() 0 3 1
A getIdGetter() 0 3 1
A getRepositoryName() 0 3 1
A getDefaultSerializedModel() 0 20 6
A setKey() 0 5 1
A getIdSerializeKey() 0 3 1
A getIdentifierAttribute() 0 13 2
A getIdKey() 0 3 1
A __construct() 0 5 1
A setRelationList() 0 5 1
A getKey() 0 3 1
A getAttributeList() 0 3 1
A setModelName() 0 5 1
A setAttributeList() 0 23 4
A setRepositoryName() 0 5 1
A getAttribute() 0 5 2
A getRelation() 0 6 4
A hasIdentifierAttribute() 0 3 1
A getRelationList() 0 3 1
1
<?php
2
3
namespace Mapado\RestClientSdk\Mapping;
4
5
use Mapado\RestClientSdk\Exception\MissingIdentifierException;
6
use Mapado\RestClientSdk\Exception\MoreThanOneIdentifierException;
7
8
/**
9
 * Class ClassMetadata
10
 *
11
 * @author Julien Deniau <[email protected]>
12
 */
13
class ClassMetadata
14
{
15
    /**
16
     * Model name (entity class with full namespace, ie: "Foo\Entity\Article").
17
     *
18
     * @var string
19
     */
20
    private $modelName;
21
22
    /**
23
     * Model key, used as path prefix for API calls.
24
     *
25
     * @var string
26
     */
27
    private $key;
28
29
    /**
30
     * Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository").
31
     *
32
     * @var string
33
     */
34
    private $repositoryName;
35
36
    /**
37
     * attributeList
38
     *
39
     * @var Attribute[]
40
     */
41
    private $attributeList;
42
43
    /**
44
     * relationList
45
     *
46
     * @var Relation[]
47
     */
48
    private $relationList;
49
50
    /**
51
     * identifierAttribute
52
     *
53
     * @var ?Attribute
54
     */
55
    private $identifierAttribute;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param string $key
61
     * @param string $modelName
62
     * @param string $repositoryName
63
     */
64
    public function __construct($key, $modelName, $repositoryName)
65
    {
66 1
        $this->key = $key;
67 1
        $this->modelName = $modelName;
68 1
        $this->repositoryName = $repositoryName;
69 1
    }
70
71
    /**
72
     * Getter for modelName
73
     *
74
     * @return string
75
     */
76
    public function getModelName()
77
    {
78
        return $this->modelName;
79
    }
80
81
    /**
82
     * Setter for modelName
83
     *
84
     * @param string $modelName
85
     *
86
     * @return ClassMetadata
87
     */
88
    public function setModelName($modelName)
89
    {
90
        $this->modelName = $modelName;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Getter for key
97
     *
98
     * @return string
99
     */
100
    public function getKey()
101
    {
102
        return $this->key;
103
    }
104
105
    /**
106
     * Setter for key
107
     *
108
     * @param string $key
109
     *
110
     * @return ClassMetadata
111
     */
112
    public function setKey($key)
113
    {
114
        $this->key = $key;
115
116
        return $this;
117
    }
118
119
    /**
120
     * getAttribute
121
     *
122
     * @param string $name
123
     *
124
     * @return ?Attribute
125
     */
126
    public function getAttribute($name)
127
    {
128
        return isset($this->attributeList[$name])
129
            ? $this->attributeList[$name]
130
            : null;
131
    }
132
133
    public function hasIdentifierAttribute(): bool
134
    {
135
        return (bool) $this->identifierAttribute;
136
    }
137
138
    /**
139
     * getIdentifierAttribute
140
     *
141
     * @return Attribute
142
     */
143
    public function getIdentifierAttribute()
144
    {
145 1
        if (!$this->identifierAttribute) {
146 1
            throw new MissingIdentifierException(
147 1
                sprintf(
148 1
                    'Ressource "%s" does not contains an identifier. You can not call %s. You may want to call `hasIdentifierAttribute` before.',
149 1
                    $this->modelName,
150 1
                    __METHOD__
151
                )
152
            );
153
        }
154
155
        return $this->identifierAttribute;
156
    }
157
158
    /**
159
     * Getter for attributeList
160
     *
161
     * @return Attribute[]
162
     */
163
    public function getAttributeList()
164
    {
165
        return $this->attributeList;
166
    }
167
168
    /**
169
     * Setter for attributeList
170
     *
171
     * @param Attribute[] $attributeList
172
     *
173
     * @return ClassMetadata
174
     */
175
    public function setAttributeList($attributeList)
176
    {
177 1
        $this->attributeList = [];
178
179 1
        foreach ($attributeList as $attribute) {
180 1
            $this->attributeList[$attribute->getSerializedKey()] = $attribute;
181
182 1
            if ($attribute->isIdentifier()) {
183 1
                if ($this->identifierAttribute) {
184 1
                    throw new MoreThanOneIdentifierException(
185 1
                        sprintf(
186 1
                            'Class metadata for model "%s" already has an identifier named "%s". Only one identifier is allowed.',
187 1
                            $this->modelName,
188 1
                            $this->identifierAttribute->getSerializedKey()
189
                        )
190
                    );
191
                }
192
193 1
                $this->identifierAttribute = $attribute;
194
            }
195
        }
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * Getter for relationList
202
     *
203
     * @return Relation[]
204
     */
205
    public function getRelationList()
206
    {
207
        return $this->relationList;
208
    }
209
210
    /**
211
     * Setter for relationList
212
     *
213
     * @param Relation[] $relationList
214
     *
215
     * @return ClassMetadata
216
     */
217
    public function setRelationList($relationList)
218
    {
219
        $this->relationList = $relationList;
220
221
        return $this;
222
    }
223
224
    /**
225
     * getRelation
226
     *
227
     * @param string $key
228
     *
229
     * @return Relation|null
230
     */
231
    public function getRelation($key)
232
    {
233
        if (!empty($this->relationList)) {
234
            foreach ($this->relationList as $relation) {
235
                if ($relation->getSerializedKey() == $key) {
236
                    return $relation;
237
                }
238
            }
239
        }
240
    }
241
242
    /**
243
     * Getter for repositoryName
244
     *
245
     * @return string
246
     */
247
    public function getRepositoryName()
248
    {
249
        return $this->repositoryName;
250
    }
251
252
    /**
253
     * Setter for repositoryName
254
     *
255
     * @param string $repositoryName
256
     *
257
     * @return ClassMetadata
258
     */
259
    public function setRepositoryName($repositoryName)
260
    {
261
        $this->repositoryName = $repositoryName;
262
263
        return $this;
264
    }
265
266
    public function getIdGetter()
267
    {
268
        return 'get' . ucfirst($this->getIdKey());
269
    }
270
271
    public function getIdSerializeKey()
272
    {
273
        return $this->getIdentifierAttribute()->getSerializedKey();
274
    }
275
276
    /**
277
     * return default serialize model with null value or empty array on relations
278
     *
279
     * @return array
280
     */
281
    public function getDefaultSerializedModel()
282
    {
283
        $out = [];
284
        $attributeList = $this->getAttributeList();
285
        if ($attributeList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributeList of type Mapado\RestClientSdk\Mapping\Attribute[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
286
            foreach ($attributeList as $attribute) {
287
                $out[$attribute->getSerializedKey()] = null;
288
            }
289
        }
290
291
        $relationList = $this->getRelationList();
292
        if ($relationList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationList of type Mapado\RestClientSdk\Mapping\Relation[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
293
            foreach ($relationList as $relation) {
294
                if ($relation->isOneToMany()) {
295
                    $out[$relation->getSerializedKey()] = [];
296
                }
297
            }
298
        }
299
300
        return $out;
301
    }
302
303
    private function getIdKey()
304
    {
305
        return $this->getIdentifierAttribute()->getAttributeName();
306
    }
307
}
308