Passed
Pull Request — master (#11)
by Pavel
08:05
created

JmsNormalizerAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 34
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A normalize() 0 11 2
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Adapters\JMS;
4
5
use Bankiru\Api\JsonRpc\NormalizerInterface;
6
use JMS\Serializer\ArrayTransformerInterface;
7
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
8
9
final class JmsNormalizerAdapter implements NormalizerInterface
10
{
11
    /** @var ArrayTransformerInterface */
12
    private $normalizer;
13
    /** @var SerializationContextFactoryInterface */
14
    private $contextFactory;
15
16
    /**
17
     * JmsNormalizerAdapter constructor.
18
     *
19
     * @param ArrayTransformerInterface            $normalizer
20
     * @param SerializationContextFactoryInterface $contextFactory
21
     */
22 8
    public function __construct(
23
        ArrayTransformerInterface $normalizer,
24
        SerializationContextFactoryInterface $contextFactory
25
    ) {
26 8
        $this->normalizer     = $normalizer;
27 8
        $this->contextFactory = $contextFactory;
28 8
    }
29
30
    /** {@inheritdoc} */
31 6
    public function normalize($entity, array $context = [])
32
    {
33 6
        if (null === $entity) {
34
            return null;
35
        }
36
37 6
        $jmsContext = $this->contextFactory->createSerializationContext();
38 6
        $jmsContext->setGroups($context);
39
40 6
        return $this->normalizer->toArray($entity, $jmsContext);
41
    }
42
}
43