Completed
Push — 6.13 ( 7e7b66...6c34c5 )
by André
53:02 queued 27:51
created

StashPass::process()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 13
nop 1
dl 0
loc 33
rs 8.7697
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the StashPass class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;
10
11
use eZ\Bundle\EzPublishCoreBundle\Cache\Driver\Redis\RedisIgbinary;
12
use eZ\Bundle\EzPublishCoreBundle\Cache\Driver\Redis\RedisIgbinaryLzf;
13
use eZ\Bundle\EzPublishCoreBundle\Cache\Driver\Redis\RedisSerializeLzf;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * This compiler pass overrides default Stash's Redis driver with one of stored in eZ/Bundle/EzPublishCoreBundle/Cache/Driver.
19
 */
20
class StashPass implements CompilerPassInterface
21
{
22
    public function process(ContainerBuilder $container)
23
    {
24
        if (!$container->hasDefinition('stash.driver')) {
25
            return;
26
        }
27
28
        $igbinary = $container->hasParameter('ezpublish.stash_cache.igbinary') ? $container->getParameter('ezpublish.stash_cache.igbinary') : false;
29
        $lzf = $container->hasParameter('ezpublish.stash_cache.lzf') ? $container->getParameter('ezpublish.stash_cache.lzf') : false;
30
31
        $config = $container->getExtensionConfig('stash');
32
        $config = reset($config);
33
        foreach ($config['caches'] as $name => $configuration) {
34
            if (in_array('Redis', $configuration['drivers'], true)) {
35
                $this->configureRedis($container, $igbinary, $lzf);
36
            }
37
        }
38
39
        $stashDriverDef = $container->findDefinition('stash.driver');
40
        $stashDriverDef->setFactory(
41
            [
42
                '%ezpublish.stash_cache.driver_factory.class%',
43
                'registerAndCreateDriver',
44
            ]
45
        );
46
        $stashDriverDef->setArguments(
47
            [
48
                '%ezpublish.stash_cache.redis_driver.name%',
49
                '%ezpublish.stash_cache.redis_driver.class%',
50
            ]
51
        );
52
        $stashDriverDef->setSynthetic(true);
53
        $stashDriverDef->setAbstract(true);
54
    }
55
56
    private function configureRedis(ContainerBuilder $container, $igbinary, $lzf)
57
    {
58
        if ($igbinary && $lzf) {
59
            $container->setParameter('ezpublish.stash_cache.redis_driver.class', RedisIgbinaryLzf::class);
60
        } elseif ($igbinary && !$lzf) {
61
            $container->setParameter('ezpublish.stash_cache.redis_driver.class', RedisIgbinary::class);
62
        } elseif (!$igbinary && $lzf) {
63
            $container->setParameter('ezpublish.stash_cache.redis_driver.class', RedisSerializeLzf::class);
64
        }
65
    }
66
}
67