Passed
Push — master ( 69aa8a...e95496 )
by Paul
03:24
created

CollectionObjectTransformer::supports()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace CCT\Component\Rest\Transformer\Response;
4
5
use CCT\Component\Rest\Transformer\AbstractSerializerTransformer;
6
use CCT\Component\Rest\Http\ResponseInterface;
7
use Symfony\Component\HttpFoundation\Response;
8
9
class CollectionObjectTransformer extends AbstractSerializerTransformer
10
{
11
    protected $mappingKeys = null;
12
13
    /**
14
     * @param ResponseInterface|Response $response
15
     *
16
     * {@inheritdoc}
17
     */
18
    public function transform(ResponseInterface $response)
19
    {
20
        $data = $this->map($response->getData());
21
        foreach ($data as $k => $object) {
22
            $data[$k] = $this->serializer->deserialize(
23
                json_encode($object),
24
                $this->class,
25
                'json',
26
                $this->context
27
            );
28
        }
29
30
        $response->setData($data);
31
    }
32
33
    /**
34
     * @param ResponseInterface|Response $response
35
     *
36
     * {@inheritdoc}
37
     */
38 2
    public function supports(ResponseInterface $response): bool
39
    {
40 2
        $data = $response->getData();
41
42
        return (
43 2
            $response->isSuccessful()
44 2
            && $this->isArrayAndNotEmpty($data)
45 2
            && $this->mappingKeysExist($data)
46 2
            && $this->isSequential($this->map($data))
47
        );
48
    }
49
50
    /**
51
     * Check if $data is an array and not empty
52
     *
53
     * @param mixed $data
54
     *
55
     * @return bool
56
     */
57 2
    protected function isArrayAndNotEmpty($data)
58
    {
59 2
        return  is_array($data) && !empty($data);
60
    }
61
62
    /**
63
     * Extract collection from response array based on mapping key.
64
     * Supports single level only
65
     * If no mapping key returns full data
66
     *
67
     * @param $data
68
     *
69
     * @return array
70
     */
71
    protected function map(array $data): array
72
    {
73
        if (null === $this->mappingKeys) {
74
            return $data;
75
        }
76
77
        foreach ($this->mappingKeys as $key) {
78
            if (key_exists($key, $data)) {
79
                return $data;
80
            }
81
        }
82
        return [];
83
    }
84
85
    protected function mappingKeysExist(array $data): bool
86
    {
87
        if (null === $this->mappingKeys) {
88
            return true;
89
        }
90
91
        return (bool) count(array_intersect($this->mappingKeys, array_flip($data))) > 0;
92
    }
93
94
    /**
95
     * @return array|null
96
     */
97
    public function getMappingKeys(): ?array
98
    {
99
        return $this->mappingKeys;
100
    }
101
102
    /**
103
     * @param null|array $mappingKeys
104
     */
105
    public function setMappingKeys(array $mappingKeys = null)
106
    {
107
        $this->mappingKeys = $mappingKeys;
108
    }
109
110
    public function isSequential($data)
111
    {
112
        return array_keys($data) === range(0, count($data) - 1);
113
    }
114
}
115