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

ObjectTransformer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 10 1
A supports() 0 6 3
A isSequential() 0 3 1
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