RegisterCachePoolPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 37
ccs 17
cts 18
cp 0.9444
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 31 5
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer bundle package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\SerializerBundle\DependencyInjection\Compiler;
13
14
use Ivory\Serializer\Mapping\Factory\CacheClassMetadataFactory;
15
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
16
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class RegisterCachePoolPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 234
    public function process(ContainerBuilder $container)
31
    {
32 234
        if (!$container->hasDefinition($classMetadataFactoryService = 'ivory.serializer.mapping.factory')) {
33 198
            return;
34
        }
35
36 36
        $classMetadataFactory = $container->getDefinition($classMetadataFactoryService);
37
38 36
        if ($classMetadataFactory->getClass() !== CacheClassMetadataFactory::class) {
39
            return;
40
        }
41
42 36
        $cache = (string) $classMetadataFactory->getArgument(1);
43
44 36
        if (class_exists(CachePoolPass::class) || $container->hasDefinition($cache)) {
45 15
            return;
46
        }
47
48 21
        $cachePath = $container->getParameter('kernel.cache_dir').'/ivory-serializer';
49
50 21
        $container->setDefinition(
51 21
            $cache = 'ivory.serializer.cache',
52 21
            new Definition(FilesystemAdapter::class, ['', 0, $cachePath])
53 15
        );
54
55 21
        $classMetadataFactory->replaceArgument(1, $cachePool = new Reference($cache));
56
57
        $container
58 21
            ->getDefinition('ivory.serializer.cache_warmer')
59 21
            ->replaceArgument(2, $cachePool);
60 21
    }
61
}
62