Completed
Push — master ( cec909...bbe7af )
by Jasper
11s queued 10s
created

Hydrator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 98.25%

Importance

Changes 0
Metric Value
wmc 20
eloc 46
dl 0
loc 161
ccs 56
cts 57
cp 0.9825
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B hydrateRelationships() 0 37 7
A hydrateItem() 0 11 2
A hydrateCollection() 0 8 2
A getCollection() 0 15 3
A getElementKey() 0 3 1
A getItemClass() 0 7 2
A getItemKey() 0 3 1
A getItem() 0 3 1
1
<?php
2
3
namespace Swis\JsonApi\Client\JsonApi;
4
5
use Art4\JsonApiClient\ElementInterface;
6
use Art4\JsonApiClient\ResourceCollectionInterface;
7
use Art4\JsonApiClient\ResourceIdentifierCollectionInterface;
8
use Art4\JsonApiClient\ResourceIdentifierInterface;
9
use Art4\JsonApiClient\ResourceItemInterface;
10
use Swis\JsonApi\Client\Collection;
11
use Swis\JsonApi\Client\Interfaces\ItemInterface;
12
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
13
use Swis\JsonApi\Client\Item;
14
15
class Hydrator
16
{
17
    /**
18
     * @var \Swis\JsonApi\Client\Interfaces\TypeMapperInterface
19
     */
20
    protected $typeMapper;
21
22
    /**
23
     * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper
24
     */
25 24
    public function __construct(TypeMapperInterface $typeMapper)
26
    {
27 24
        $this->typeMapper = $typeMapper;
28 24
    }
29
30
    /**
31
     * @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
32
     *
33
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
34
     */
35 24
    public function hydrateItem(ResourceItemInterface $jsonApiItem): ItemInterface
36
    {
37 24
        $item = $this->getItemClass($jsonApiItem->get('type'));
38
39 24
        $item->setId($jsonApiItem->get('id'));
40
41 24
        if ($jsonApiItem->has('attributes')) {
42 24
            $item->fill($jsonApiItem->get('attributes')->asArray(true));
43
        }
44
45 24
        return $item;
46
    }
47
48
    /**
49
     * @param \Art4\JsonApiClient\ResourceCollectionInterface $jsonApiCollection
50
     *
51
     * @return \Swis\JsonApi\Client\Collection
52
     */
53 3
    public function hydrateCollection(ResourceCollectionInterface $jsonApiCollection): Collection
54
    {
55 3
        $collection = new Collection();
56 3
        foreach ($jsonApiCollection->asArray() as $item) {
57 3
            $collection->push($this->hydrateItem($item));
58
        }
59
60 3
        return $collection;
61
    }
62
63
    /**
64
     * @param \Swis\JsonApi\Client\Collection $jsonApiItems
65
     * @param \Swis\JsonApi\Client\Collection $items
66
     */
67 15
    public function hydrateRelationships(Collection $jsonApiItems, Collection $items)
68
    {
69 15
        $keyedItems = $items->reverse()->keyBy(
70 15
            function (ItemInterface $item) {
71 15
                return $this->getItemKey($item);
72 15
            }
73
        );
74
75 15
        $jsonApiItems->each(
76 15
            function (ResourceItemInterface $jsonApiItem) use ($keyedItems) {
77 15
                if (!$jsonApiItem->has('relationships')) {
78 12
                    return;
79
                }
80
81 15
                $item = $this->getItem($keyedItems, $jsonApiItem);
82
83 15
                if ($item === null) {
84
                    return;
85
                }
86
87 15
                foreach ($jsonApiItem->get('relationships')->asArray() as $name => $relationship) {
88
                    /** @var \Art4\JsonApiClient\ElementInterface $data */
89 15
                    $data = $relationship->get('data');
90 15
                    $method = camel_case($name);
91
92 15
                    if ($data instanceof ResourceIdentifierInterface) {
93 15
                        $includedItem = $this->getItem($keyedItems, $data);
94
95 15
                        if ($includedItem === null) {
96 15
                            continue;
97
                        }
98
99 9
                        $item->setRelation($method, $includedItem);
100 15
                    } elseif ($data instanceof ResourceIdentifierCollectionInterface) {
101 15
                        $collection = $this->getCollection($keyedItems, $data);
102
103 15
                        $item->setRelation($method, $collection);
104
                    }
105
                }
106 15
            }
107
        );
108 15
    }
109
110
    /**
111
     * @param string $type
112
     *
113
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
114
     */
115 24
    protected function getItemClass(string $type): ItemInterface
116
    {
117 24
        if ($this->typeMapper->hasMapping($type)) {
118 18
            return $this->typeMapper->getMapping($type);
119
        }
120
121 6
        return (new Item())->setType($type);
122
    }
123
124
    /**
125
     * @param \Swis\JsonApi\Client\Collection                                                           $included
126
     * @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceItemInterface $identifier
127
     *
128
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
129
     */
130 15
    protected function getItem(Collection $included, $identifier)
131
    {
132 15
        return $included->get($this->getElementKey($identifier));
133
    }
134
135
    /**
136
     * @param \Swis\JsonApi\Client\Collection                                                                           $included
137
     * @param \Art4\JsonApiClient\ResourceIdentifierCollectionInterface|\Art4\JsonApiClient\ResourceCollectionInterface $identifierCollection
138
     *
139
     * @return \Swis\JsonApi\Client\Collection
140
     */
141 15
    protected function getCollection(Collection $included, $identifierCollection): Collection
142
    {
143 15
        $items = new Collection();
144
145 15
        foreach ($identifierCollection->asArray() as $identifier) {
146 15
            $item = $this->getItem($included, $identifier);
147
148 15
            if ($item === null) {
149 15
                continue;
150
            }
151
152 6
            $items->push($item);
153
        }
154
155 15
        return $items;
156
    }
157
158
    /**
159
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
160
     *
161
     * @return string
162
     */
163 15
    protected function getItemKey(ItemInterface $item): string
164
    {
165 15
        return sprintf('%s:%s', $item->getType(), $item->getId());
166
    }
167
168
    /**
169
     * @param \Art4\JsonApiClient\ElementInterface $accessor
170
     *
171
     * @return string
172
     */
173 15
    protected function getElementKey(ElementInterface $accessor): string
174
    {
175 15
        return sprintf('%s:%s', $accessor->get('type'), $accessor->get('id'));
176
    }
177
}
178