Completed
Push — http_cache_mvc ( 64ff00...c340cf )
by
unknown
29:34
created

enableCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator;
7
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * A ResponseCacheConfigurator configurable by constructor arguments.
12
 */
13
class ConfigurableResponseCacheConfigurator implements ResponseCacheConfigurator
14
{
15
    /**
16
     * True if view cache is enabled, false if it is not.
17
     *
18
     * @var bool
19
     */
20
    private $enableViewCache;
21
22
    /**
23
     * True if TTL cache is enabled, false if it is not.
24
     * @var bool
25
     */
26
    private $enableTtlCache;
27
28
    /**
29
     * Default ttl for ttl cache.
30
     *
31
     * @var int
32
     */
33
    private $defaultTtl;
34
35
    public function __construct($enableViewCache, $enableTtlCache, $defaultTtl)
36
    {
37
        $this->enableViewCache = $enableViewCache;
38
        $this->enableTtlCache = $enableTtlCache;
39
        $this->defaultTtl = $defaultTtl;
40
    }
41
42
    public function enableCache(Response $response)
43
    {
44
        if ($this->enableViewCache) {
45
            $response->setPublic();
46
        }
47
48
        return $this;
49
    }
50
51
    public function setSharedMaxAge(Response $response)
52
    {
53
        if ($this->enableViewCache && $this->enableTtlCache && !$response->headers->hasCacheControlDirective('s-maxage')) {
54
            $response->setSharedMaxAge($this->defaultTtl);
55
        }
56
57
        return $this;
58
    }
59
60
    public function addTags(Response $response, $tags)
61
    {
62
        if ($this->enableViewCache) {
63
            $response->headers->set('xkey', $tags, false);
64
        }
65
66
        return $this;
67
    }
68
}
69