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

JmsNormalizerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
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