Completed
Push — http_cache_purge_client-bc ( f79d9f )
by
unknown
17:56
created

FOSPurgeClient::purge()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the FOSClient 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\Publish\Core\MVC\Symfony\Cache\Http;
10
11
use eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface;
12
use eZ\Publish\Core\MVC\Symfony\Cache\TagAwarePurgeClientInterface;
13
use FOS\HttpCacheBundle\CacheManager;
14
15
/**
16
 * Purge client based on FOSHttpCacheBundle.
17
 *
18
 * Only support BAN requests on purpose, to be able to invalidate cache for a
19
 * collection of Location/Content objects.
20
 */
21
class FOSPurgeClient implements PurgeClientInterface
22
{
23
    /**
24
     * @var CacheManager
25
     */
26
    private $cacheManager;
27
28
    public function __construct(CacheManager $cacheManager)
29
    {
30
        $this->cacheManager = $cacheManager;
31
    }
32
33
    public function __destruct()
34
    {
35
        $this->cacheManager->flush();
36
    }
37
38
    public function purge($tags)
39
    {
40
        if (empty($tags)) {
41
            return;
42
        }
43
44
        // As xkey only support one tag (key) being invalidated at a time, we loop.
45
        // These will be queued by FOS\HttpCache\ProxyClient\Varnish and handled on kernel.terminate.
46
        foreach (array_unique((array)$tags) as $tag) {
47
            if (is_numeric($tag)) {
48
                $tag = 'location-' . $tag;
49
            }
50
51
            $this->cacheManager->invalidatePath(
52
                '/',
53
                ['xkey' => $tag, 'Host' => empty($_SERVER['SERVER_NAME']) ? 'localhost' : $_SERVER['SERVER_NAME']]
54
            );
55
        }
56
    }
57
58
    public function purgeAll()
59
    {
60
        $this->cacheManager->invalidate(['xkey' => '.*']);
61
    }
62
}
63