RegisterCachePoolPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A 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\Component\Cache\Adapter\FilesystemAdapter;
16
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
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
    public function process(ContainerBuilder $container)
31
    {
32
        if (!$container->hasDefinition($classMetadataFactoryService = 'ivory.serializer.mapping.factory')) {
33
            return;
34
        }
35
36
        $classMetadataFactory = $container->getDefinition($classMetadataFactoryService);
37
38
        if (CacheClassMetadataFactory::class !== $classMetadataFactory->getClass()) {
39
            return;
40
        }
41
42
        $cache = (string) $classMetadataFactory->getArgument(1);
43
44
        if (class_exists(CachePoolPass::class) || $container->hasDefinition($cache)) {
45
            return;
46
        }
47
48
        $cachePath = $container->getParameter('kernel.cache_dir').'/ivory-serializer';
49
50
        $container->setDefinition(
51
            $cache = 'ivory.serializer.cache',
52
            new Definition(FilesystemAdapter::class, ['', 0, $cachePath])
53
        );
54
55
        $classMetadataFactory->replaceArgument(1, $cachePool = new Reference($cache));
56
57
        $container
58
            ->getDefinition('ivory.serializer.cache_warmer')
59
            ->replaceArgument(2, $cachePool);
60
    }
61
}
62