ObjectTransformer::supports()   A
last analyzed

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 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 3
rs 10
c 2
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
use function \count;
0 ignored issues
show
introduced by
The function \count was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
9
10
class ObjectTransformer extends AbstractSerializerResponseTransformer
11
{
12
    /**
13
     * @param ResponseInterface|Response $response
14
     *
15
     * {@inheritdoc}
16
     */
17 4
    public function transform(ResponseInterface $response, ContextInterface $context = null): void
18
    {
19 4
        $data = $this->serializer->deserialize(
20 4
            $response->getContent(),
21 4
            $this->class,
22 4
            'json',
23 4
            $context ?? $this->context
24
        );
25
26 4
        $response->setData($data);
27 4
    }
28
29
    /**
30
     * @param ResponseInterface|Response $response
31
     *
32
     * {@inheritdoc}
33
     */
34 5
    public function supports(ResponseInterface $response): bool
35
    {
36
        return
37 5
            $response->isSuccessful()
38 5
            && !empty($response->getData())
39 5
            && false === $this->isSequential($response->getData());
40
    }
41
42
    /**
43
     * @param array $data
44
     *
45
     * @return bool
46
     */
47 3
    protected function isSequential(array $data): bool
48
    {
49 3
        return array_keys($data) === range(0, count($data) - 1);
50
    }
51
}
52