Test Failed
Pull Request — master (#11)
by Pavel
23:42
created

JmsNormalizerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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
    public function __construct(
23
        ArrayTransformerInterface $normalizer,
24
        SerializationContextFactoryInterface $contextFactory
25
    ) {
26
        $this->normalizer     = $normalizer;
27
        $this->contextFactory = $contextFactory;
28
    }
29
30
    /** {@inheritdoc} */
31
    public function normalize($entity, array $context = [])
32
    {
33
        if (null === $entity) {
34
            return null;
35
        }
36
37
        $jmsContext = $this->contextFactory->createSerializationContext();
38
        $jmsContext->setGroups($context);
39
40
        return $this->normalizer->toArray($entity, $jmsContext);
41
    }
42
}
43