Completed
Pull Request — master (#52)
by Jasper
04:02
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
namespace Swis\JsonApi\Client;
4
5
use Swis\JsonApi\Client\Interfaces\DataInterface;
6
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
7
use Swis\JsonApi\Client\Interfaces\ItemInterface;
8
9
class DocumentFactory
10
{
11
    /**
12
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
13
     *
14
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
15
     */
16 45
    public function make(DataInterface $data): DocumentInterface
17
    {
18 45
        $document = new Document();
19 45
        if ($data instanceof ItemInterface) {
20 35
            $document = new ItemDocument();
21 10
        } elseif ($data instanceof Collection) {
22 10
            $document = new CollectionDocument();
23
        }
24
25 45
        return $document->setData($data)->setIncluded($this->getIncluded($data));
26
    }
27
28
    /**
29
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
30
     *
31
     * @return \Swis\JsonApi\Client\Collection
32
     */
33 45
    private function getIncluded(DataInterface $data): Collection
34
    {
35 45
        return Collection::wrap($data)
36 45
            ->flatMap(
37 18
                function (ItemInterface $item) {
38 45
                    return $this->getIncludedFromItem($item);
39 45
                }
40
            )
41 45
            ->unique(
42 18
                static function (ItemInterface $item) {
43 20
                    return sprintf('%s:%s', $item->getType(), $item->getId());
44 45
                }
45
            )
46 45
            ->values();
47
    }
48
49
    /**
50
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
51
     *
52
     * @return \Swis\JsonApi\Client\Collection
53
     */
54 45
    private function getIncludedFromItem(ItemInterface $item): Collection
55
    {
56 45
        return Collection::make($item->getRelations())
57 45
            ->reject(
58 18
                static function ($relationship) {
59
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
60 35
                    return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
61 45
                }
62
            )
63 45
            ->flatMap(
64 18
                static function ($relationship) {
65
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
66 35
                    return Collection::wrap($relationship->getIncluded());
67 45
                }
68
            )
69 45
            ->flatMap(
70 18
                function (ItemInterface $item) {
71 35
                    return Collection::wrap($item)->merge($this->getIncludedFromItem($item));
72 45
                }
73
            )
74 45
            ->filter(
75 18
                static function (ItemInterface $item) {
76 35
                    return $item->hasType() && $item->hasId() && ($item->hasAttributes() || $item->hasRelationships());
77 45
                }
78
            );
79
    }
80
}
81