Completed
Branch next (29fadd)
by Neomerx
03:15
created

Factory::createParsedIdentifier()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0109

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 69
ccs 7
cts 9
cp 0.7778
crap 1.0109
rs 8.6763
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Factory.php$1 ➔ __construct() 0 7 1
A Factory.php$1 ➔ getType() 0 4 1
A Factory.php$1 ➔ getId() 0 4 1
A Factory.php$1 ➔ hasIdentifierMeta() 0 4 1
A Factory.php$1 ➔ getIdentifierMeta() 0 4 1
A Factory.php$1 ➔ getPosition() 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Factories;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
22
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
23
use Neomerx\JsonApi\Contracts\Http\Headers\AcceptMediaTypeInterface;
24
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
25
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface as ParserIdentifierInterface;
26
use Neomerx\JsonApi\Contracts\Parser\ParserInterface;
27
use Neomerx\JsonApi\Contracts\Parser\PositionInterface;
28
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
29
use Neomerx\JsonApi\Contracts\Parser\RelationshipInterface;
30
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface;
31
use Neomerx\JsonApi\Contracts\Representation\DocumentWriterInterface;
32
use Neomerx\JsonApi\Contracts\Representation\ErrorWriterInterface;
33
use Neomerx\JsonApi\Contracts\Representation\FieldSetFilterInterface;
34
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface as SchemaIdentifierInterface;
35
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
36
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
37
use Neomerx\JsonApi\Encoder\Encoder;
38
use Neomerx\JsonApi\Http\Headers\AcceptMediaType;
39
use Neomerx\JsonApi\Http\Headers\MediaType;
40
use Neomerx\JsonApi\Parser\IdentifierAndResource;
41
use Neomerx\JsonApi\Parser\Parser;
42
use Neomerx\JsonApi\Representation\DocumentWriter;
43
use Neomerx\JsonApi\Representation\ErrorWriter;
44
use Neomerx\JsonApi\Representation\FieldSetFilter;
45
use Neomerx\JsonApi\Schema\Link;
46
use Neomerx\JsonApi\Schema\SchemaContainer;
47
48
/**
49
 * @package Neomerx\JsonApi
50
 */
51
class Factory implements FactoryInterface
52
{
53
    /**
54
     * @inheritdoc
55
     */
56 72
    public function createEncoder(SchemaContainerInterface $container): EncoderInterface
57
    {
58 72
        return new Encoder($this, $container);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 77
    public function createSchemaContainer(iterable $schemas): SchemaContainerInterface
65
    {
66 77
        return new SchemaContainer($this, $schemas);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 67
    public function createPosition(
73
        int $level,
74
        string $path,
75
        ?string $parentType,
76
        ?string $parentRelationship
77
    ): PositionInterface {
78
        return new class ($level, $path, $parentType, $parentRelationship) implements PositionInterface
79
        {
80
            /**
81
             * @var int
82
             */
83
            private $level;
84
85
            /**
86
             * @var string
87
             */
88
            private $path;
89
90
            /**
91
             * @var null|string
92
             */
93
            private $parentType;
94
95
            /**
96
             * @var null|string
97
             */
98
            private $parentRelationship;
99
100
            /**
101
             * @param int         $level
102
             * @param string      $path
103
             * @param null|string $parentType
104
             * @param null|string $parentRelationship
105
             */
106
            public function __construct(int $level, string $path, ?string $parentType, ?string $parentRelationship)
107
            {
108 67
                $this->level              = $level;
109 67
                $this->path               = $path;
110 67
                $this->parentType         = $parentType;
111 67
                $this->parentRelationship = $parentRelationship;
112 67
            }
113
114
            /**
115
             * @inheritdoc
116
             */
117
            public function getLevel(): int
118
            {
119 59
                return $this->level;
120
            }
121
122
            /**
123
             * @inheritdoc
124
             */
125
            public function getPath(): string
126
            {
127 59
                return $this->path;
128
            }
129
130
            /**
131
             * @inheritdoc
132
             */
133
            public function getParentType(): ?string
134
            {
135 21
                return $this->parentType;
136
            }
137
138
            /**
139
             * @inheritdoc
140
             */
141
            public function getParentRelationship(): ?string
142
            {
143 7
                return $this->parentRelationship;
144
            }
145
        };
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 67
    public function createParser(SchemaContainerInterface $container): ParserInterface
152
    {
153 67
        return new Parser($this, $container);
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 69
    public function createDocumentWriter(): DocumentWriterInterface
160
    {
161 69
        return new DocumentWriter($this);
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167 7
    public function createErrorWriter(): ErrorWriterInterface
168
    {
169 7
        return new ErrorWriter($this);
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 66
    public function createFieldSetFilter(array $fieldSets): FieldSetFilterInterface
176
    {
177 66
        return new FieldSetFilter($fieldSets);
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183 59
    public function createParsedResource(
184
        PositionInterface $position,
185
        SchemaContainerInterface $container,
186
        $data
187
    ): ResourceInterface {
188 59
        return new IdentifierAndResource($position, $this, $container, $data);
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194 1
    public function createParsedIdentifier(
195
        PositionInterface $position,
196
        SchemaIdentifierInterface $identifier
197
    ): ParserIdentifierInterface {
198
        return new class ($position, $identifier) implements ParserIdentifierInterface
199
        {
200
            /**
201
             * @var PositionInterface
202
             */
203
            private $position;
204
205
            /**
206
             * @var SchemaIdentifierInterface
207
             */
208
            private $identifier;
209
210
            /**
211
             * @param PositionInterface         $position
212
             * @param SchemaIdentifierInterface $identifier
213
             */
214
            public function __construct(
215
                PositionInterface $position,
216
                SchemaIdentifierInterface $identifier
217
            ) {
218 1
                $this->position   = $position;
219 1
                $this->identifier = $identifier;
220 1
            }
221
222
            /**
223
             * @inheritdoc
224
             */
225
            public function getType(): string
226
            {
227 1
                return $this->identifier->getType();
228
            }
229
230
            /**
231
             * @inheritdoc
232
             */
233
            public function getId(): ?string
234
            {
235 1
                return $this->identifier->getId();
236
            }
237
238
            /**
239
             * @inheritdoc
240
             */
241
            public function hasIdentifierMeta(): bool
242
            {
243 1
                return $this->identifier->hasIdentifierMeta();
244
            }
245
246
            /**
247
             * @inheritdoc
248
             */
249
            public function getIdentifierMeta()
250
            {
251
                return $this->identifier->getIdentifierMeta();
252
            }
253
254
            /**
255
             * @inheritdoc
256
             */
257
            public function getPosition(): PositionInterface
258
            {
259
                return $this->position;
260
            }
261
        };
262
    }
263
264
    /**
265
     * @inheritdoc
266
     */
267 61
    public function createLink(bool $isSubUrl, string $value, bool $hasMeta, $meta = null): LinkInterface
268
    {
269 61
        return new Link($isSubUrl, $value, $hasMeta, $meta);
270
    }
271
272
    /**
273
     * @inheritdoc
274
     */
275
    public function createRelationship(
276
        PositionInterface $position,
277
        bool $hasData,
278
        ?RelationshipDataInterface $data,
279
        bool $hasLinks,
280
        ?iterable $links,
281
        bool $hasMeta,
282
        $meta
283
    ): RelationshipInterface {
284
        return new class (
285
            $position,
286
            $hasData,
287
            $data,
288
            $hasLinks,
289
            $links,
290
            $hasMeta,
291
            $meta
292
        ) implements RelationshipInterface
293
        {
294
            /**
295
             * @var PositionInterface
296
             */
297
            private $position;
298
299
            /**
300
             * @var bool
301
             */
302
            private $hasData;
303
304
            /**
305
             * @var ?RelationshipDataInterface
306
             */
307
            private $data;
308
309
            /**
310
             * @var bool
311
             */
312
            private $hasLinks;
313
314
            /**
315
             * @var ?iterable
316
             */
317
            private $links;
318
319
            /**
320
             * @var bool
321
             */
322
            private $hasMeta;
323
324
            /**
325
             * @var mixed
326
             */
327
            private $meta;
328
329
            /**
330
             * @var bool
331
             */
332
            private $metaIsCallable;
333
334
            /**
335
             * @param PositionInterface              $position
336
             * @param bool                           $hasData
337
             * @param RelationshipDataInterface|null $data
338
             * @param bool                           $hasLinks
339
             * @param iterable|null                  $links
340
             * @param bool                           $hasMeta
341
             * @param mixed                          $meta
342
             */
343 44
            public function __construct(
344
                PositionInterface $position,
345
                bool $hasData,
346
                ?RelationshipDataInterface $data,
347
                bool $hasLinks,
348
                ?iterable $links,
349
                bool $hasMeta,
350
                $meta
351
            ) {
352 44
                assert($position->getLevel() > ParserInterface::ROOT_LEVEL);
353 44
                assert(empty($position->getPath()) === false);
354 44
                assert(($hasData === false && $data === null) || ($hasData === true && $data !== null));
355 44
                assert(($hasLinks === false && $links === null) || ($hasLinks === true && $links !== null));
356
357 44
                $this->position       = $position;
358 44
                $this->hasData        = $hasData;
359 44
                $this->data           = $data;
360 44
                $this->hasLinks       = $hasLinks;
361 44
                $this->links          = $links;
362 44
                $this->hasMeta        = $hasMeta;
363 44
                $this->meta           = $meta;
364 44
                $this->metaIsCallable = is_callable($meta);
365 44
            }
366
367
            /**
368
             * @inheritdoc
369
             */
370 44
            public function getPosition(): PositionInterface
371
            {
372 44
                return $this->position;
373
            }
374
375
            /**
376
             * @inheritdoc
377
             */
378 44
            public function hasData(): bool
379
            {
380 44
                return $this->hasData;
381
            }
382
383
            /**
384
             * @inheritdoc
385
             */
386 33
            public function getData(): RelationshipDataInterface
387
            {
388 33
                assert($this->hasData());
389
390 33
                return $this->data;
391
            }
392
393
            /**
394
             * @inheritdoc
395
             */
396 38
            public function hasLinks(): bool
397
            {
398 38
                return $this->hasLinks;
399
            }
400
401
            /**
402
             * @inheritdoc
403
             */
404 21
            public function getLinks(): iterable
405
            {
406 21
                assert($this->hasLinks());
407
408 21
                return $this->links;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->links; of type Neomerx\JsonApi\Factories\iterable|null adds the type Neomerx\JsonApi\Factories\iterable to the return on line 408 which is incompatible with the return type declared by the interface Neomerx\JsonApi\Contract...shipInterface::getLinks of type Neomerx\JsonApi\Contracts\Parser\iterable.
Loading history...
409
            }
410
411
            /**
412
             * @inheritdoc
413
             */
414 38
            public function hasMeta(): bool
415
            {
416 38
                return $this->hasMeta;
417
            }
418
419
            /**
420
             * @inheritdoc
421
             */
422 1
            public function getMeta()
423
            {
424 1
                assert($this->hasMeta());
425
426 1
                if ($this->metaIsCallable === true) {
427 1
                    $this->meta           = call_user_func($this->meta);
428 1
                    $this->metaIsCallable = false;
429
                }
430
431 1
                return $this->meta;
432
            }
433
        };
434
    }
435
436
    /**
437
     * @inheritdoc
438
     */
439 4
    public function createMediaType(string $type, string $subType, array $parameters = null): MediaTypeInterface
440
    {
441 4
        return new MediaType($type, $subType, $parameters);
442
    }
443
444
    /**
445
     * @inheritdoc
446
     */
447 12
    public function createAcceptMediaType(
448
        int $position,
449
        string $type,
450
        string $subType,
451
        array $parameters = null,
452
        float $quality = 1.0
453
    ): AcceptMediaTypeInterface {
454 12
        return new AcceptMediaType($position, $type, $subType, $parameters, $quality);
455
    }
456
}
457