Completed
Push — 6.7 ( 470b16...7283a5 )
by André
13:35
created

HttpCachePass::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the HttpCachePass 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\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface;
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use InvalidArgumentException;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * HttpCache related compiler pass.
19
 *
20
 * Ensures Varnish proxy client is correctly configured.
21
 */
22
class HttpCachePass implements CompilerPassInterface
23
{
24
    public function process(ContainerBuilder $container)
25
    {
26
        $this->processCacheManager($container);
27
        $this->processPurgeClient($container);
28
    }
29
30
    private function processCacheManager(ContainerBuilder $container)
31
    {
32
        if (!$container->hasDefinition('ezpublish.http_cache.cache_manager')) {
33
            return;
34
        }
35
36
        if (!$container->hasDefinition('fos_http_cache.proxy_client.varnish')) {
37
            throw new InvalidArgumentException('Varnish proxy client must be enabled in FOSHttpCacheBundle');
38
        }
39
40
        $varnishClientDef = $container->findDefinition('fos_http_cache.proxy_client.varnish');
41
        $varnishClientDef->setFactory(
42
            [
43
                new Reference('ezpublish.http_cache.proxy_client.varnish.factory'),
44
                'buildProxyClient',
45
            ]
46
        );
47
        // Set it lazy as it can be loaded during cache warming and factory depends on ConfigResolver while cache warming
48
        // occurs before SA matching.
49
        $varnishClientDef->setLazy(true);
50
51
        // Forcing cache manager to use Varnish proxy client, for BAN support.
52
        $cacheManagerDef = $container->findDefinition('ezpublish.http_cache.cache_manager');
53
        $cacheManagerDef->replaceArgument(0, new Reference('fos_http_cache.proxy_client.varnish'));
54
    }
55
56
    private function processPurgeClient(ContainerBuilder $container)
57
    {
58
        // Check that alias exists (if not it has been removed by another bundle)
59
        if (!$container->has('ezpublish.http_cache.purge_client')) {
60
            return;
61
        }
62
63
        $purgeType = $container->getParameter('ezpublish.http_cache.purge_type');
64
        switch ($purgeType) {
65
            case 'local':
66
                $purgeService = 'ezpublish.http_cache.purge_client.local';
67
                break;
68
            case 'http':
69
                $purgeService = 'ezpublish.http_cache.purge_client.fos';
70
                break;
71
            default:
72
                if (!$container->has($purgeType)) {
73
                    throw new InvalidArgumentException("Invalid ezpublish.http_cache.purge_type. Can be 'local', 'http' or a valid service identifier implementing PurgeClientInterface.");
74
                } elseif (!$container->get($purgeType) instanceof PurgeClientInterface) {
75
                    throw new InvalidArgumentException('Invalid ezpublish.http_cache.purge_type, it needs to implement PurgeClientInterface.');
76
                }
77
78
                $purgeService = $purgeType;
79
        }
80
81
        $container->setAlias('ezpublish.http_cache.purge_client', $purgeService);
82
    }
83
}
84