Completed
Push — http_cache_purge_client ( 8388e6...dc2277 )
by
unknown
32:08 queued 18:40
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 FOS\HttpCacheBundle\CacheManager;
13
14
/**
15
 * Purge client based on FOSHttpCacheBundle.
16
 *
17
 * Only support BAN requests on purpose, to be able to invalidate cache for a
18
 * collection of Location/Content objects.
19
 */
20
class FOSPurgeClient implements PurgeClientInterface
21
{
22
    /**
23
     * @var CacheManager
24
     */
25
    private $cacheManager;
26
27
    public function __construct(CacheManager $cacheManager)
28
    {
29
        $this->cacheManager = $cacheManager;
30
    }
31
32
    public function __destruct()
33
    {
34
        $this->cacheManager->flush();
35
    }
36
37
    public function purge($tags)
38
    {
39
        if (empty($tags)) {
40
            return;
41
        }
42
43
        // As key only support one tag being invalidated at a time, we loop.
44
        // These will be queued by FOS\HttpCache\ProxyClient\Varnish and handled on kernel.terminate.
45
        foreach (array_unique((array)$tags) as $tag) {
46
            if (is_numeric($tag)) {
47
                $tag = 'location-' . $tag;
48
            }
49
50
            $this->cacheManager->invalidatePath(
51
                '/',
52
                ['key' => $tag, 'Host' => empty($_SERVER['SERVER_NAME']) ? 'localhost' : $_SERVER['SERVER_NAME']]
53
            );
54
        }
55
    }
56
57
    public function purgeAll()
58
    {
59
        $this->cacheManager->invalidate(['key' => '.*']);
60
    }
61
}
62