Completed
Push — master ( 1319dc...6e6413 )
by Lukas Kahwe
05:12
created

SerializerConfigurationPass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 47
ccs 25
cts 30
cp 0.8333
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 44 10
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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * Checks if a serializer is either set or can be auto-configured.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 * @author Florian Voutzinos <[email protected]>
22
 *
23
 * @internal
24
 */
25
final class SerializerConfigurationPass implements CompilerPassInterface
26
{
27 13
    public function process(ContainerBuilder $container)
28
    {
29 13
        if ($container->has('fos_rest.serializer')) {
30 1
            $class = $container->getParameterBag()->resolveValue(
31 1
                $container->findDefinition('fos_rest.serializer')->getClass()
32 1
            );
33 1
            if (!is_subclass_of($class, 'FOS\RestBundle\Serializer\Serializer')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
34
                throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "%s" given).', $class));
35
            }
36
37 1
            return;
38
        }
39
40 12
        if (!$container->has('serializer') && !$container->has('jms_serializer.serializer')) {
41 1
            throw new \InvalidArgumentException('Neither a service called "jms_serializer.serializer" nor "serializer" is available and no serializer is explicitly configured. You must either enable the JMSSerializerBundle, enable the FrameworkBundle serializer or configure a custom serializer.');
42
        }
43
44 11
        if ($container->has('serializer')) {
45 10
            $class = $container->getParameterBag()->resolveValue(
46 10
                $container->findDefinition('serializer')->getClass()
47 10
            );
48
49 10
            if (is_subclass_of($class, 'Symfony\Component\Serializer\SerializerInterface')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
50 8
                $container->setAlias('fos_rest.serializer', 'fos_rest.serializer.symfony');
51 8
                $container->removeDefinition('fos_rest.serializer.exception_wrapper_serialize_handler');
52 10
            } elseif (is_subclass_of($class, 'JMS\Serializer\SerializerInterface')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
53 2
                $container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms');
54 2
            } elseif (is_subclass_of($class, 'FOS\RestBundle\Serializer\Serializer')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
55
                $container->setAlias('fos_rest.serializer', 'serializer');
56
            } else {
57
                throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "%s" given).', $class));
58
            }
59
60 10
            return;
61
        } else {
62 1
            $container->removeDefinition('fos_rest.serializer.exception_wrapper_normalizer');
63
        }
64
65 1
        if ($container->has('jms_serializer.serializer')) {
66 1
            $container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms');
67 1
        } else {
68
            $container->removeDefinition('fos_rest.serializer.exception_wrapper_serialize_handler');
69
        }
70 1
    }
71
}
72