Completed
Push — master ( 73467e...b02c72 )
by Eric
129:04 queued 64:14
created

RegisterCachePoolPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 33.33%

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 6
cts 18
cp 0.3333
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 36
            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