SerializerConfigurationPass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 39
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 36 8
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\Serializer;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\Serializer\SerializerInterface;
18
19
/**
20
 * Checks if a serializer is either set or can be auto-configured.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 * @author Florian Voutzinos <[email protected]>
24
 *
25
 * @internal
26
 */
27
final class SerializerConfigurationPass implements CompilerPassInterface
28
{
29 19
    public function process(ContainerBuilder $container): void
30
    {
31 19
        if ($container->has('fos_rest.serializer')) {
32 1
            $class = $container->getParameterBag()->resolveValue(
33 1
                $container->findDefinition('fos_rest.serializer')->getClass()
34
            );
35 1
            if (!is_subclass_of($class, Serializer::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FOS\RestBundle\Serializer\Serializer::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
36
                throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement %s (instance of "%s" given).', Serializer::class, $class));
37
            }
38
39 1
            return;
40
        }
41
42 18
        if (!$container->has('serializer') && !$container->has('jms_serializer.serializer')) {
43 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.');
44
        }
45
46 17
        if ($container->has('jms_serializer.serializer')) {
47 7
            $container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms');
48
49 7
            return;
50
        }
51
52
        // As there is no `jms_serializer.serializer` service, there is a `serializer` service
53 10
        $class = $container->getParameterBag()->resolveValue(
54 10
            $container->findDefinition('serializer')->getClass()
55
        );
56
57 10
        if (is_subclass_of($class, SerializerInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Symfony\Component\Seria...ializerInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
58 10
            $container->setAlias('fos_rest.serializer', 'fos_rest.serializer.symfony');
59
        } elseif (is_subclass_of($class, Serializer::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FOS\RestBundle\Serializer\Serializer::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
60
            $container->setAlias('fos_rest.serializer', 'serializer');
61
        } else {
62
            throw new \InvalidArgumentException(sprintf('The class of the "serializer" service in use is not supported (instance of "%s" given). Please make it implement %s or configure the service "fos_rest.serializer" with a class implementing %s.', $class, Serializer::class, Serializer::class));
63
        }
64 10
    }
65
}
66