Completed
Push — master ( cc21f4...3eeaf5 )
by Julien
06:41 queued 03:06
created

ModelHydrator::guessCollectionClassname()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Model;
4
5
use Mapado\RestClientSdk\Collection\Collection;
6
use Mapado\RestClientSdk\Collection\HalCollection;
7
use Mapado\RestClientSdk\Collection\HydraPaginatedCollection;
8
use Mapado\RestClientSdk\Helper\ArrayHelper;
9
use Mapado\RestClientSdk\SdkClient;
10
11
/**
12
 * Class ModelHydrator
13
 *
14
 * @author Julien Deniau <[email protected]>
15
 */
16
class ModelHydrator
17
{
18
    /**
19
     * sdk
20
     *
21
     * @var SdkClient
22
     */
23
    protected $sdk;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param SdkClient $sdk
29
     */
30
    public function __construct(SdkClient $sdk)
31
    {
32 1
        $this->sdk = $sdk;
33 1
    }
34
35
    /**
36
     * convertId
37
     *
38
     * @param mixed $id
39
     * @param string $modelName
40
     *
41
     * @return string
42
     */
43
    public function convertId($id, $modelName)
44
    {
45 1
        $id = (string) $id;
46
47
        // add slash if needed to have a valid hydra id
48 1
        if (false === strpos($id, '/')) {
49 1
            $mapping = $this->sdk->getMapping();
50 1
            $key = $mapping->getKeyFromModel($modelName);
51 1
            $id = '/' . $key . '/' . $id;
52
53 1
            if ($prefix = $mapping->getIdPrefix()) {
54 1
                $id = $prefix . $id;
55
            }
56
        }
57
58 1
        return $id;
59
    }
60
61
    /**
62
     * convert data as array to entity
63
     *
64
     * @param array|null $data
65
     * @param string $modelName
66
     *
67
     * @return object|null
68
     */
69
    public function hydrate($data, $modelName)
70
    {
71 1
        $mapping = $this->sdk->getMapping();
72 1
        $key = $mapping->getKeyFromModel($modelName);
73 1
        $modelName = $mapping->getModelName($key);
74
75 1
        return $this->deserialize($data, $modelName);
76
    }
77
78
    /**
79
     * convert API response to Collection containing entities
80
     *
81
     * @param array|null $data
82
     * @param string $modelName
83
     *
84
     * @return Collection
85
     */
86
    public function hydrateList($data, $modelName)
87
    {
88 1
        $collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey'];
89
90 1
        if (is_array($data) && ArrayHelper::arrayHas($data, $collectionKey)) {
91 1
            return $this->deserializeAll($data, $modelName);
92
        }
93
94
        return new Collection();
95
    }
96
97
    /**
98
     * convert list of data as array to Collection containing entities
99
     *
100
     * @param array  $data
101
     * @param string $modelName
102
     *
103
     * @return Collection
104
     */
105
    private function deserializeAll($data, $modelName)
106
    {
107 1
        $collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey'];
108
109 1
        $itemList = array_map(function ($member) use ($modelName) {
110 1
            return $this->deserialize($member, $modelName);
111 1
        }, ArrayHelper::arrayGet($data, $collectionKey));
0 ignored issues
show
Bug introduced by
It seems like Mapado\RestClientSdk\Hel...($data, $collectionKey) can also be of type null; however, parameter $arr1 of array_map() does only seem to accept array, 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

111
        }, /** @scrutinizer ignore-type */ ArrayHelper::arrayGet($data, $collectionKey));
Loading history...
112
113 1
        $extraProperties = array_filter(
114 1
            $data,
115 1
            function ($key) use ($collectionKey) {
116 1
                return $key !== $collectionKey;
117 1
            },
118 1
            ARRAY_FILTER_USE_KEY
119
        );
120
121 1
        $collectionClassName = $this->guessCollectionClassname($data);
122
123 1
        return new $collectionClassName($itemList, $extraProperties);
124
    }
125
126
    /**
127
     * convert array to entity
128
     *
129
     * @param array|null $data
130
     * @param string $modelName
131
     *
132
     * @return object|null
133
     */
134
    private function deserialize($data, $modelName)
135
    {
136 1
        if (!is_array($data)) {
137
            return null;
138
        }
139
140 1
        return $this->sdk->getSerializer()->deserialize($data, $modelName);
141
    }
142
143
    /**
144
     * guess collection classname according to response data
145
     *
146
     * @param array $data
147
     *
148
     * @return string
149
     */
150
    private function guessCollectionClassname($data)
151
    {
152
        switch (true) {
153 1
            case !empty($data['@type']) &&
154 1
            'hydra:PagedCollection' === $data['@type']:
155 1
                return HydraPaginatedCollection::class;
156
157 1
            case array_key_exists('_embedded', $data):
158 1
                return HalCollection::class;
159
160
            default:
161
                return Collection::class;
162
        }
163
    }
164
}
165