SerializerNormalizerPass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace Bdf\SerializerBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9
10
/**
11
 * Registers all service tag as normalizer into the serializer.
12
 */
13
class SerializerNormalizerPass implements CompilerPassInterface
14
{
15
    use PriorityTaggedServiceTrait;
16
17
    private $service;
18
    private $normalizerTag;
19
20 4
    public function __construct(string $service = 'bdf_serializer.normalizer.loader', string $normalizerTag = 'bdf_serializer.normalizer')
21
    {
22 4
        $this->service = $service;
23 4
        $this->normalizerTag = $normalizerTag;
24
    }
25
26 3
    public function process(ContainerBuilder $container)
27
    {
28 3
        if (!$container->hasDefinition($this->service)) {
29
            return;
30
        }
31
32 3
        if (!$normalizers = $this->findAndSortTaggedServices($this->normalizerTag, $container)) {
33 1
            throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->normalizerTag, $this->service));
34
        }
35
36 2
        $serializerDefinition = $container->getDefinition($this->service);
37 2
        $serializerDefinition->replaceArgument(0, $normalizers);
38
    }
39
}
40