Completed
Pull Request — master (#1483)
by Antoine
03:47
created

ResourceMetadata   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 4
cbo 0
dl 0
loc 282
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getShortName() 0 4 1
A withShortName() 0 7 1
A getDescription() 0 4 1
A withDescription() 0 7 1
A getIri() 0 4 1
A withIri() 0 7 1
A getCollectionOperationAttribute() 0 4 1
A getItemOperationAttribute() 0 4 1
A getItemOperations() 0 4 1
A withItemOperations() 0 7 1
A getCollectionOperations() 0 4 1
A withCollectionOperations() 0 7 1
A getSubresourceOperations() 0 4 1
A withSubresourceOperations() 0 7 1
A getSubresourceOperationAttribute() 0 4 1
B getOperationAttribute() 0 12 5
A getAttributes() 0 4 1
A getAttribute() 0 8 2
A withAttributes() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Metadata\Resource;
15
16
/**
17
 * Resource metadata.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21
final class ResourceMetadata
22
{
23
    private $shortName;
24
    private $description;
25
    private $iri;
26
    private $itemOperations;
27
    private $collectionOperations;
28
    private $subresourceOperations;
29
    private $attributes;
30
31
    public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null, array $subresourceOperations = null)
32
    {
33
        $this->shortName = $shortName;
34
        $this->description = $description;
35
        $this->iri = $iri;
36
        $this->itemOperations = $itemOperations;
37
        $this->collectionOperations = $collectionOperations;
38
        $this->subresourceOperations = $subresourceOperations;
39
        $this->attributes = $attributes;
40
    }
41
42
    /**
43
     * Gets the short name.
44
     *
45
     * @return string|null
46
     */
47
    public function getShortName()
48
    {
49
        return $this->shortName;
50
    }
51
52
    /**
53
     * Returns a new instance with the given short name.
54
     *
55
     * @param string $shortName
56
     *
57
     * @return self
58
     */
59
    public function withShortName(string $shortName): self
60
    {
61
        $metadata = clone $this;
62
        $metadata->shortName = $shortName;
63
64
        return $metadata;
65
    }
66
67
    /**
68
     * Gets the description.
69
     *
70
     * @return string|null
71
     */
72
    public function getDescription()
73
    {
74
        return $this->description;
75
    }
76
77
    /**
78
     * Returns a new instance with the given description.
79
     *
80
     * @param string $description
81
     *
82
     * @return self
83
     */
84
    public function withDescription(string $description): self
85
    {
86
        $metadata = clone $this;
87
        $metadata->description = $description;
88
89
        return $metadata;
90
    }
91
92
    /**
93
     * Gets the associated IRI.
94
     *
95
     * @return string|null
96
     */
97
    public function getIri()
98
    {
99
        return $this->iri;
100
    }
101
102
    /**
103
     * Returns a new instance with the given IRI.
104
     *
105
     * @param string $iri
106
     *
107
     * @return self
108
     */
109
    public function withIri(string $iri): self
110
    {
111
        $metadata = clone $this;
112
        $metadata->iri = $iri;
113
114
        return $metadata;
115
    }
116
117
    /**
118
     * Gets item operations.
119
     *
120
     * @return array|null
121
     */
122
    public function getItemOperations()
123
    {
124
        return $this->itemOperations;
125
    }
126
127
    /**
128
     * Returns a new instance with the given item operations.
129
     *
130
     * @param array $itemOperations
131
     *
132
     * @return self
133
     */
134
    public function withItemOperations(array $itemOperations): self
135
    {
136
        $metadata = clone $this;
137
        $metadata->itemOperations = $itemOperations;
138
139
        return $metadata;
140
    }
141
142
    /**
143
     * Gets collection operations.
144
     *
145
     * @return array|null
146
     */
147
    public function getCollectionOperations()
148
    {
149
        return $this->collectionOperations;
150
    }
151
152
    /**
153
     * Returns a new instance with the given collection operations.
154
     *
155
     * @param array $collectionOperations
156
     *
157
     * @return self
158
     */
159
    public function withCollectionOperations(array $collectionOperations): self
160
    {
161
        $metadata = clone $this;
162
        $metadata->collectionOperations = $collectionOperations;
163
164
        return $metadata;
165
    }
166
167
    /**
168
     * Gets subresource operations.
169
     *
170
     * @return array|null
171
     */
172
    public function getSubresourceOperations()
173
    {
174
        return $this->subresourceOperations;
175
    }
176
177
    /**
178
     * Returns a new instance with the given subresource operations.
179
     *
180
     * @param array $subresourceOperations
181
     *
182
     * @return self
183
     */
184
    public function withSubresourceOperations(array $subresourceOperations): self
185
    {
186
        $metadata = clone $this;
187
        $metadata->subresourceOperations = $subresourceOperations;
188
189
        return $metadata;
190
    }
191
192
    /**
193
     * Gets a collection operation attribute, optionally fallback to a resource attribute.
194
     *
195
     * @param string|null $operationName
196
     * @param string      $key
197
     * @param mixed       $defaultValue
198
     * @param bool        $resourceFallback
199
     *
200
     * @return mixed
201
     */
202
    public function getCollectionOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
203
    {
204
        return $this->getOperationAttribute($this->collectionOperations, $operationName, $key, $defaultValue, $resourceFallback);
205
    }
206
207
    /**
208
     * Gets an item operation attribute, optionally fallback to a resource attribute.
209
     *
210
     * @param string|null $operationName
211
     * @param string      $key
212
     * @param mixed       $defaultValue
213
     * @param bool        $resourceFallback
214
     *
215
     * @return mixed
216
     */
217
    public function getItemOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
218
    {
219
        return $this->getOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback);
220
    }
221
222
    /**
223
     * Gets a subresource operation attribute, optionally fallback to a resource attribute.
224
     *
225
     * @param string|null $operationName
226
     * @param string      $key
227
     * @param mixed       $defaultValue
228
     * @param bool        $resourceFallback
229
     *
230
     * @return mixed
231
     */
232
    public function getSubresourceOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
233
    {
234
        return $this->getOperationAttribute($this->subresourceOperations, $operationName, $key, $defaultValue, $resourceFallback);
235
    }
236
237
    /**
238
     * Gets an operation attribute, optionally fallback to a resource attribute.
239
     *
240
     * @param array|null  $operations
241
     * @param string|null $operationName
242
     * @param string      $key
243
     * @param mixed       $defaultValue
244
     * @param bool        $resourceFallback
245
     *
246
     * @return mixed
247
     */
248
    private function getOperationAttribute(array $operations = null, string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
249
    {
250
        if (null !== $operationName && isset($operations[$operationName][$key])) {
251
            return $operations[$operationName][$key];
252
        }
253
254
        if ($resourceFallback && isset($this->attributes[$key])) {
255
            return $this->attributes[$key];
256
        }
257
258
        return $defaultValue;
259
    }
260
261
    /**
262
     * Gets attributes.
263
     *
264
     * @return array|null
265
     */
266
    public function getAttributes()
267
    {
268
        return $this->attributes;
269
    }
270
271
    /**
272
     * Gets an attribute.
273
     *
274
     * @param string $key
275
     * @param mixed  $defaultValue
276
     *
277
     * @return mixed
278
     */
279
    public function getAttribute(string $key, $defaultValue = null)
280
    {
281
        if (isset($this->attributes[$key])) {
282
            return $this->attributes[$key];
283
        }
284
285
        return $defaultValue;
286
    }
287
288
    /**
289
     * Returns a new instance with the given attribute.
290
     *
291
     * @param array $attributes
292
     *
293
     * @return self
294
     */
295
    public function withAttributes(array $attributes): self
296
    {
297
        $metadata = clone $this;
298
        $metadata->attributes = $attributes;
299
300
        return $metadata;
301
    }
302
}
303