Passed
Push — master ( 24d2dd...159fd0 )
by Paul
04:04
created

ObjectTransformer::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace CCT\Component\Rest\Transformer\Response;
4
5
use CCT\Component\Rest\Http\Response;
6
use CCT\Component\Rest\Http\ResponseInterface;
7
use CCT\Component\Rest\Serializer\ContextInterface;
8
9
class ObjectTransformer extends AbstractSerializerResponseTransformer
10
{
11
    /**
12
     * @param ResponseInterface|Response $response
13
     *
14
     * {@inheritdoc}
15
     */
16 4
    public function transform(ResponseInterface $response, ContextInterface $context = null): void
17
    {
18 4
        $data = $this->serializer->deserialize(
19 4
            $response->getContent(),
20 4
            $this->class,
21 4
            'json',
22 4
            $context ?? $this->context
23
        );
24
25 4
        $response->setData($data);
26 4
    }
27
28
    /**
29
     * @param ResponseInterface|Response $response
30
     *
31
     * {@inheritdoc}
32
     */
33 5
    public function supports(ResponseInterface $response): bool
34
    {
35
        return
36 5
            $response->isSuccessful()
37 5
            && !empty($response->getData())
38 5
            && false === $this->isSequential($response->getData());
39
    }
40
41
    /**
42
     * @param array $data
43
     *
44
     * @return bool
45
     */
46 3
    protected function isSequential(array $data): bool
47
    {
48 3
        return array_keys($data) === range(0, count($data) - 1);
49
    }
50
}
51