Passed
Push — master ( 3e882a...fac044 )
by Sébastien
15:53 queued 07:32
created

BdfSerializerExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 61
ccs 21
cts 22
cp 0.9545
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 9 1
A configureCache() 0 8 3
A createCacheReference() 0 14 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
    }
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
    }
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
64 1
            return new Reference($namespace);
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 5
    public function getConfiguration(array $config, ContainerBuilder $container)
74
    {
75 5
        return new Configuration();
76
    }
77
}