Completed
Push — http_cache_mvc ( 5374b9 )
by
unknown
27:11
created

ResponseCacheConfigurator::setSharedMaxAge()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
7
namespace eZ\Publish\Core\MVC\Symfony\Cache\Http;
8
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Configures caching options of an HTTP Response.
13
 */
14
class ResponseCacheConfigurator
15
{
16
    /**
17
     * Enables cache on a Response.
18
     *
19
     * @param \Symfony\Component\HttpFoundation\Response $response
20
     *
21
     * @return self
22
     */
23
    public function enableCache(Response $response)
24
    {
25
        $response->setPublic();
26
27
        return $this;
28
    }
29
30
    /**
31
     * Sets the shared-max-age property of a Response.
32
     *
33
     * @param \Symfony\Component\HttpFoundation\Response $response
34
     * @param int $ttl
35
     * @param bool $replace If true, any existing value will be replaced
36
     *
37
     * @return self
38
     */
39
    public function setSharedMaxAge(Response $response, $ttl, $replace = false)
40
    {
41
        if (!$response->headers->hasCacheControlDirective('s-maxage') || $replace === true) {
42
            $response->setSharedMaxAge($ttl);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Adds $tags to the response's cache tags header.
50
     *
51
     * @param \Symfony\Component\HttpFoundation\Response $response
52
     * @param string|array $tags Single tag, or array of tags
53
     *
54
     * @return self
55
     */
56
    public function addTags(Response $response, $tags)
57
    {
58
        $response->headers->set('xkey', $tags, false);
59
60
        return $this;
61
    }
62
}
63