SerializerNormalizerPass::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
ccs 6
cts 7
cp 0.8571
crap 3.0261
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