IncludedExtension::hydrate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 6
cts 10
cp 0.6
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3.576
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Hydrator\Extension;
5
6
use Mikemirten\Component\JsonApi\Document\Behaviour\IncludedResourcesAwareInterface;
7
use Mikemirten\Component\JsonApi\Hydrator\DocumentHydrator;
8
9
/**
10
 * "included" extension
11
 *
12
 * @see http://jsonapi.org/format/#document-compound-documents
13
 *
14
 * @package Mikemirten\Component\JsonApi\Hydrator\Extension
15
 */
16 View Code Duplication
class IncludedExtension implements ExtensionInterface
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function hydrate($object, $source, DocumentHydrator $hydrator)
22
    {
23 2
        if (! $object instanceof IncludedResourcesAwareInterface) {
24
            throw new InvalidDocumentException(sprintf(
25
                'Given instance of "%s" does not implements "%s"',
26
                get_class($object),
27
                IncludedResourcesAwareInterface::class
28
            ));
29
        }
30
31 2
        foreach ($source as $item)
32
        {
33 1
            $resource = $hydrator->hydrateResource($item);
34
35 1
            $object->addIncludedResource($resource);
36
        }
37 2
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function supports(): array
43
    {
44 1
        return ['included'];
45
    }
46
}