DocumentFactory::getIncluded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
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
     * @return \Swis\JsonApi\Client\ItemDocument|\Swis\JsonApi\Client\CollectionDocument
16
     */
17 56
    public function make(DataInterface $data): DocumentInterface
18
    {
19 56
        if ($data instanceof ItemInterface) {
20 48
            $document = new ItemDocument;
21 8
        } elseif ($data instanceof Collection) {
22 8
            $document = new CollectionDocument;
23
        } else {
24
            throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data)));
25
        }
26
27 56
        return $document->setData($data)->setIncluded($this->getIncluded($data));
28
    }
29
30 56
    private function getIncluded(DataInterface $data): Collection
31
    {
32 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

32
        return Collection::wrap(/** @scrutinizer ignore-type */ $data)
Loading history...
33 56
            ->flatMap(fn (ItemInterface $item) => $this->getIncludedFromItem($item))
34 56
            ->unique(static fn (ItemInterface $item) => sprintf('%s:%s', $item->getType(), $item->getId()))
35 56
            ->values();
36
    }
37
38 56
    private function getIncludedFromItem(ItemInterface $item): Collection
39
    {
40 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

40
        return Collection::make(/** @scrutinizer ignore-type */ $item->getRelations())
Loading history...
41 56
            ->reject(
42 56
                static function ($relationship) {
43
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
44 40
                    return $relationship->shouldOmitIncluded() || ! $relationship->hasIncluded();
45 56
                }
46 28
            )
47 56
            ->flatMap(
48 56
                static function ($relationship) {
49
                    /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
50 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

50
                    return Collection::wrap(/** @scrutinizer ignore-type */ $relationship->getIncluded());
Loading history...
51 56
                }
52 28
            )
53 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

53
            ->flatMap(fn (ItemInterface $item) => Collection::wrap(/** @scrutinizer ignore-type */ $item)->merge($this->getIncludedFromItem($item)))
Loading history...
54 56
            ->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item));
55
    }
56
57 36
    private function itemCanBeIncluded(ItemInterface $item): bool
58
    {
59 36
        return $item->hasType()
60 36
            && $item->hasId()
61 36
            && ($item->hasAttributes() || $item->hasRelationships());
62
    }
63
}
64