Passed
Push — master ( fa6633...b8fff6 )
by Jasper
21:11 queued 10:46
created

DocumentFactory::getIncludedFromItem()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 1
nop 1
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 2
rs 9.8333
c 1
b 0
f 0
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 70
    public function make(DataInterface $data): DocumentInterface
20
    {
21 70
        if ($data instanceof ItemInterface) {
22 60
            $document = new ItemDocument();
23 10
        } elseif ($data instanceof Collection) {
24 10
            $document = new CollectionDocument();
25
        } else {
26
            throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data)));
27
        }
28
29 70
        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 70
    private function getIncluded(DataInterface $data): Collection
38
    {
39 70
        return Collection::wrap($data)
0 ignored issues
show
Bug introduced by
$data of type Swis\JsonApi\Client\Interfaces\DataInterface is incompatible with the type iterable expected by parameter $value of Illuminate\Support\Collection::wrap(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        return Collection::wrap(/** @scrutinizer ignore-type */ $data)
Loading history...
40 70
            ->flatMap(
41 70
                function (ItemInterface $item) {
42 70
                    return $this->getIncludedFromItem($item);
43
                }
44
            )
45 70
            ->unique(
46 70
                static function (ItemInterface $item) {
47 30
                    return sprintf('%s:%s', $item->getType(), $item->getId());
48
                }
49
            )
50 70
            ->values();
51
    }
52
53
    /**
54
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
55
     *
56
     * @return \Swis\JsonApi\Client\Collection
57
     */
58 70
    private function getIncludedFromItem(ItemInterface $item): Collection
59
    {
60 70
        return Collection::make($item->getRelations())
0 ignored issues
show
Bug introduced by
$item->getRelations() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return Collection::make(/** @scrutinizer ignore-type */ $item->getRelations())
Loading history...
61 70
            ->reject(
62 70
                static function ($relationship) {
63
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
64 50
                    return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
65
                }
66
            )
67 70
            ->flatMap(
68 70
                static function ($relationship) {
69
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
70 45
                    return Collection::wrap($relationship->getIncluded());
0 ignored issues
show
Bug introduced by
It seems like $relationship->getIncluded() can also be of type Swis\JsonApi\Client\Interfaces\ItemInterface; however, parameter $value of Illuminate\Support\Collection::wrap() does only seem to accept iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                    return Collection::wrap(/** @scrutinizer ignore-type */ $relationship->getIncluded());
Loading history...
71
                }
72
            )
73 70
            ->flatMap(
74 70
                function (ItemInterface $item) {
75 45
                    return Collection::wrap($item)->merge($this->getIncludedFromItem($item));
0 ignored issues
show
Bug introduced by
$item of type Swis\JsonApi\Client\Interfaces\ItemInterface is incompatible with the type iterable expected by parameter $value of Illuminate\Support\Collection::wrap(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
                    return Collection::wrap(/** @scrutinizer ignore-type */ $item)->merge($this->getIncludedFromItem($item));
Loading history...
76
                }
77
            )
78 70
            ->filter(
79 70
                function (ItemInterface $item) {
80 45
                    return $this->itemCanBeIncluded($item);
81
                }
82
            );
83
    }
84
85
    /**
86
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
87
     *
88
     * @return bool
89
     */
90 45
    private function itemCanBeIncluded(ItemInterface $item): bool
91
    {
92 45
        return $item->hasType()
93 45
            && $item->hasId()
94 45
            && ($item->hasAttributes() || $item->hasRelationships());
95
    }
96
}
97