Passed
Push — master ( 295d69...f45702 )
by Jasper
02:00
created

DocumentFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 67
ccs 27
cts 28
cp 0.9643
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 11 3
A getIncluded() 0 6 1
A itemCanBeIncluded() 0 5 4
A getIncludedFromItem() 0 17 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(fn (ItemInterface $item) => $this->getIncludedFromItem($item))
41 56
            ->unique(static fn (ItemInterface $item) => sprintf('%s:%s', $item->getType(), $item->getId()))
42 56
            ->values();
43
    }
44
45
    /**
46
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
47
     *
48
     * @return \Swis\JsonApi\Client\Collection
49
     */
50 56
    private function getIncludedFromItem(ItemInterface $item): Collection
51
    {
52 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

52
        return Collection::make(/** @scrutinizer ignore-type */ $item->getRelations())
Loading history...
53 56
            ->reject(
54 56
                static function ($relationship) {
55
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
56 40
                    return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
57 28
                }
58
            )
59 56
            ->flatMap(
60 56
                static function ($relationship) {
61
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
62 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

62
                    return Collection::wrap(/** @scrutinizer ignore-type */ $relationship->getIncluded());
Loading history...
63 28
                }
64
            )
65 56
            ->flatMap(fn (ItemInterface $item) => 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

65
            ->flatMap(fn (ItemInterface $item) => Collection::wrap(/** @scrutinizer ignore-type */ $item)->merge($this->getIncludedFromItem($item)))
Loading history...
66 56
            ->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item));
67
    }
68
69
    /**
70
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
71
     *
72
     * @return bool
73
     */
74 36
    private function itemCanBeIncluded(ItemInterface $item): bool
75
    {
76 36
        return $item->hasType()
77 36
            && $item->hasId()
78 36
            && ($item->hasAttributes() || $item->hasRelationships());
79
    }
80
}
81