Completed
Pull Request — master (#88)
by Julien
02:57
created

ClassMetadata   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Test Coverage

Coverage 40.68%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 233
ccs 24
cts 59
cp 0.4068
rs 9.92
c 0
b 0
f 0
wmc 31

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