Passed
Push — master ( a71cc4...1302d4 )
by Vincent
21:00 queued 11:57
created

BdfSerializerExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 62
rs 10
ccs 24
cts 25
cp 0.96
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createCacheReference() 0 15 3
A load() 0 9 1
A configureCache() 0 8 3
A getConfiguration() 0 3 1
1
<?php
2
3
namespace Bdf\SerializerBundle\DependencyInjection;
4
5
use Symfony\Component\Cache\Psr16Cache;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\Extension;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
/**
13
 * SerializerExtension
14
 */
15
class BdfSerializerExtension extends Extension
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 5
    public function load(array $configs, ContainerBuilder $container)
21
    {
22 5
        $configuration = $this->getConfiguration($configs, $container);
23 5
        $config = $this->processConfiguration($configuration, $configs);
24
25 5
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
26 5
        $loader->load('serializer.yaml');
27
28 5
        $this->configureCache($config, $container);
29 5
    }
30
31
    /**
32
     * @param array $config
33
     * @param ContainerBuilder $container
34
     */
35 5
    public function configureCache(array $config, ContainerBuilder $container)
36
    {
37 5
        if (isset($config['cache'])) {
38 2
            $ref = $this->createCacheReference('bdf_serializer.cache', $config['cache'], $container);
39
40 2
            if ($ref !== null) {
41 2
                $serializerDefinition = $container->getDefinition('bdf_serializer.metadata_factory');
42 2
                $serializerDefinition->replaceArgument(1, $ref);
43
            }
44
        }
45 5
    }
46
47
    /**
48
     * @param string $namespace
49
     * @param array $config
50
     * @param ContainerBuilder $container
51
     *
52
     * @return null|Reference
53
     */
54 2
    private function createCacheReference(string $namespace, array $config, ContainerBuilder $container): ?Reference
55
    {
56 2
        if (isset($config['service'])) {
57 1
            return new Reference($config['service']);
58
        }
59
60 1
        if (isset($config['pool'])) {
61 1
            $definition = $container->register($namespace, Psr16Cache::class);
62 1
            $definition->addArgument(new Reference($config['pool']));
63 1
            $definition->setPrivate(true);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Depend...efinition::setPrivate() has been deprecated: since Symfony 5.2, use setPublic() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
            /** @scrutinizer ignore-deprecated */ $definition->setPrivate(true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
65 1
            return new Reference($namespace);
66
        }
67
68
        return null;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 5
    public function getConfiguration(array $config, ContainerBuilder $container)
75
    {
76 5
        return new Configuration();
77
    }
78
}