Passed
Push — master ( 975c2f...d51421 )
by Jasper
13:00 queued 01:21
created

DocumentFactory::getIncluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\JsonApi\Client;
6
7
use Swis\JsonApi\Client\Exceptions\UnsupportedDataException;
8
use Swis\JsonApi\Client\Interfaces\DataInterface;
9
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
10
use Swis\JsonApi\Client\Interfaces\ItemInterface;
11
12
class DocumentFactory
13
{
14
    /**
15
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
16
     *
17
     * @return \Swis\JsonApi\Client\ItemDocument|\Swis\JsonApi\Client\CollectionDocument
18
     */
19 42
    public function make(DataInterface $data): DocumentInterface
20
    {
21 42
        if ($data instanceof ItemInterface) {
22 36
            $document = new ItemDocument();
23 6
        } elseif ($data instanceof Collection) {
24 6
            $document = new CollectionDocument();
25
        } else {
26
            throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data)));
27
        }
28
29 42
        return $document->setData($data)->setIncluded($this->getIncluded($data));
30
    }
31
32
    /**
33
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
34
     *
35
     * @return \Swis\JsonApi\Client\Collection
36
     */
37 42
    private function getIncluded(DataInterface $data): Collection
38
    {
39 42
        return Collection::wrap($data)
40 42
            ->flatMap(
41 42
                function (ItemInterface $item) {
42 42
                    return $this->getIncludedFromItem($item);
43 42
                }
44
            )
45 42
            ->unique(
46 42
                static function (ItemInterface $item) {
47 18
                    return sprintf('%s:%s', $item->getType(), $item->getId());
48 42
                }
49
            )
50 42
            ->values();
51
    }
52
53
    /**
54
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
55
     *
56
     * @return \Swis\JsonApi\Client\Collection
57
     */
58 42
    private function getIncludedFromItem(ItemInterface $item): Collection
59
    {
60 42
        return Collection::make($item->getRelations())
61 42
            ->reject(
62 42
                static function ($relationship) {
63
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
64 30
                    return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
65 42
                }
66
            )
67 42
            ->flatMap(
68 42
                static function ($relationship) {
69
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
70 27
                    return Collection::wrap($relationship->getIncluded());
71 42
                }
72
            )
73 42
            ->flatMap(
74 42
                function (ItemInterface $item) {
75 27
                    return Collection::wrap($item)->merge($this->getIncludedFromItem($item));
76 42
                }
77
            )
78 42
            ->filter(
79 42
                function (ItemInterface $item) {
80 27
                    return $this->itemCanBeIncluded($item);
81 42
                }
82
            );
83
    }
84
85
    /**
86
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
87
     *
88
     * @return bool
89
     */
90 27
    private function itemCanBeIncluded(ItemInterface $item): bool
91
    {
92 27
        return $item->hasType()
93 27
            && $item->hasId()
94 27
            && ($item->hasAttributes() || $item->hasRelationships());
95
    }
96
}
97