Completed
Pull Request — master (#252)
by David
08:15 queued 03:54
created

ResponseTagger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 1
cbo 2
dl 0
loc 88
ccs 15
cts 16
cp 0.9375
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTagsHeaderName() 0 4 1
A getTagsHeaderValue() 0 4 1
A hasTags() 0 4 1
A addTags() 0 6 1
A tagResponse() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache;
13
14
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Service for Response cache tagging.
19
 *
20
 * @author David de Boer <[email protected]>
21
 * @author David Buchmann <[email protected]>
22
 * @author André Rømcke <[email protected]>
23
 */
24
class ResponseTagger
25
{
26
    /**
27
     * @var TagsInterface
28
     */
29
    private $client;
30
31
    /**
32
     * @var array
33
     */
34
    private $tags = [];
35
36
    /**
37
     * Constructor
38
     *
39
     * @param TagsInterface $client
40
     */
41 2
    public function __construct(TagsInterface $client)
42
    {
43 2
        $this->client = $client;
44 2
    }
45
46
    /**
47
     * Get the HTTP header name that will hold cache tags.
48
     *
49
     * @return string
50
     */
51 1
    public function getTagsHeaderName()
52
    {
53 1
        return $this->client->getTagsHeaderName();
54
    }
55
56
    /**
57
     * Get the value for the HTTP tag header.
58
     *
59
     * This concatenates all tags and ensures correct encoding.
60
     *
61
     * @return string
62
     */
63 2
    public function getTagsHeaderValue()
64
    {
65 2
        return $this->client->getTagsHeaderValue($this->tags);
66
    }
67
68
    /**
69
     * Check whether the tag handler has any tags to set on the response.
70
     *
71
     * @return bool True if this handler will set at least one tag.
72
     */
73 1
    public function hasTags()
74
    {
75 1
        return 0 < count($this->tags);
76
    }
77
78
    /**
79
     * Add tags to be set on the response.
80
     *
81
     * This must be called before any HTTP response is sent to the client.
82
     *
83
     * @param array $tags List of tags to add.
84
     *
85
     * @return $this
86
     */
87 2
    public function addTags(array $tags)
88
    {
89 2
        $this->tags = array_merge($this->tags, $tags);
90
91 2
        return $this;
92
    }
93
94
    /**
95
     * Set tags on a response
96
     *
97
     * @param ResponseInterface $response Original response
98
     * @param bool              $replace  Whether to replace the current tags
99
     *                                    on the response
100
     *
101
     * @return ResponseInterface          Tagged response
102
     */
103 1
    public function tagResponse(ResponseInterface $response, $replace = false)
104
    {
105 1
        if ($replace) {
106 1
            return $response->withHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue());
107
        }
108
109
        return $response->withAddedHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue());
110
    }
111
}
112