Passed
Push — master ( 8e5320...2ae03e )
by Jasper
03:08 queued 01:14
created

DocumentFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 97.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 83
ccs 39
cts 40
cp 0.975
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 11 3
A getIncluded() 0 14 1
A itemCanBeIncluded() 0 5 4
A getIncludedFromItem() 0 23 2
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 56
    public function make(DataInterface $data): DocumentInterface
20
    {
21 56
        if ($data instanceof ItemInterface) {
22 48
            $document = new ItemDocument();
23 8
        } elseif ($data instanceof Collection) {
24 8
            $document = new CollectionDocument();
25
        } else {
26
            throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data)));
27
        }
28
29 56
        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 56
    private function getIncluded(DataInterface $data): Collection
38
    {
39 56
        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 56
            ->flatMap(
41 56
                function (ItemInterface $item) {
42 56
                    return $this->getIncludedFromItem($item);
43 28
                }
44
            )
45 56
            ->unique(
46 56
                static function (ItemInterface $item) {
47 24
                    return sprintf('%s:%s', $item->getType(), $item->getId());
48 28
                }
49
            )
50 56
            ->values();
51
    }
52
53
    /**
54
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
55
     *
56
     * @return \Swis\JsonApi\Client\Collection
57
     */
58 56
    private function getIncludedFromItem(ItemInterface $item): Collection
59
    {
60 56
        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 56
            ->reject(
62 56
                static function ($relationship) {
63
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
64 40
                    return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
65 28
                }
66
            )
67 56
            ->flatMap(
68 56
                static function ($relationship) {
69
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
70 36
                    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 28
                }
72
            )
73 56
            ->flatMap(
74 56
                function (ItemInterface $item) {
75 36
                    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 28
                }
77
            )
78 56
            ->filter(
79 56
                function (ItemInterface $item) {
80 36
                    return $this->itemCanBeIncluded($item);
81 28
                }
82
            );
83
    }
84
85
    /**
86
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
87
     *
88
     * @return bool
89
     */
90 36
    private function itemCanBeIncluded(ItemInterface $item): bool
91
    {
92 36
        return $item->hasType()
93 36
            && $item->hasId()
94 36
            && ($item->hasAttributes() || $item->hasRelationships());
95
    }
96
}
97