Completed
Push — master ( 007cba...a69311 )
by Christian
01:55 queued 11s
created

HandlerRegistryDecorationPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 25
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 22 6
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\DependencyInjection\Compiler;
13
14
use FOS\RestBundle\Serializer\JMSHandlerRegistry;
15
use FOS\RestBundle\Serializer\JMSHandlerRegistryV2;
16
use JMS\Serializer\Visitor\SerializationVisitorInterface;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * Decorates the handler registry from JMSSerializerBundle.
23
 *
24
 * The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that
25
 * custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services
26
 * have been resolved.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 *
30
 * @internal
31
 */
32
class HandlerRegistryDecorationPass implements CompilerPassInterface
33
{
34
    public function process(ContainerBuilder $container): void
35
    {
36
        if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
37
            return;
38
        }
39
40
        $jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry');
41
        $public = $jmsHandlerRegistry->isPublic();
42
        $jmsHandlerRegistry->setPublic(false);
43
        $container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry);
44
45
        $fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', interface_exists(SerializationVisitorInterface::class) ? JMSHandlerRegistryV2::class : JMSHandlerRegistry::class)
46
            ->setPublic($public)
47
            ->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner'));
48
49
        // remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitionPass)
50
        foreach ($container->getDefinitions() as $id => $definition) {
51
            if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of 'fos_rest.serializer.jms_handler_registry.inner' (string) and $id (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
52
                $container->setDefinition($id, $fosRestHandlerRegistry);
53
            }
54
        }
55
    }
56
}
57