Completed
Pull Request — master (#252)
by David
04:27
created

ResponseTagger::tagResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
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\Exception\InvalidTagException;
15
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * Service for Response cache tagging.
21
 *
22
 * @author David de Boer <[email protected]>
23
 * @author David Buchmann <[email protected]>
24
 * @author André Rømcke <[email protected]>
25
 * @author Wicliff Wolda <[email protected]>
26
 */
27
class ResponseTagger
28
{
29
30
    /**
31
     * @var array
32
     */
33
    private $options;
34
35
    /**
36
     * @var TagsInterface
37
     */
38
    private $client;
39
40
    /**
41
     * @var array
42
     */
43
    private $tags = [];
44
45
    /**
46
     * Constructor
47
     *
48
     * @param TagsInterface $client
49
     * @param array         $options supported options:
50
     *                               - strict (bool) Default: false. If set to true, throws exception when adding empty tags.
51
     */
52 4
    public function __construct(TagsInterface $client, array $options = array())
53
    {
54 4
        $this->client = $client;
55
56 4
        $resolver = new OptionsResolver();
57 4
        $resolver->setDefaults(array(
58 4
            'strict' => false,
59 4
        ));
60
61 4
        $resolver->setAllowedTypes('strict', 'bool');
62
63 4
        $this->options = $resolver->resolve($options);
64 4
    }
65
66
    /**
67
     * Get the HTTP header name that will hold cache tags.
68
     *
69
     * @return string
70
     */
71 1
    public function getTagsHeaderName()
72
    {
73 1
        return $this->client->getTagsHeaderName();
74
    }
75
76
    /**
77
     * Get the value for the HTTP tag header.
78
     *
79
     * This concatenates all tags and ensures correct encoding.
80
     *
81
     * @return string
82
     */
83 3
    public function getTagsHeaderValue()
84
    {
85 3
        return $this->client->getTagsHeaderValue($this->tags);
86
    }
87
88
    /**
89
     * Check whether the tag handler has any tags to set on the response.
90
     *
91
     * @return bool True if this handler will set at least one tag.
92
     */
93 2
    public function hasTags()
94
    {
95 2
        return 0 < count($this->tags);
96
    }
97
98
    /**
99
     * Add tags to be set on the response.
100
     *
101
     * This must be called before any HTTP response is sent to the client.
102
     *
103
     * @param array $tags List of tags to add.
104
     *
105
     * @throws InvalidTagException
106
     *
107
     * @return $this
108
     */
109 4
    public function addTags(array $tags)
110
    {
111 4
        $filtered = array_filter($tags, 'strlen');
112
113 4
        if ($this->options['strict'] && array_diff($tags, $filtered)) {
114 1
            throw new InvalidTagException('Empty tags are not allowed');
115
        }
116
117 3
        $this->tags = array_merge($this->tags, $filtered);
118
119 3
        return $this;
120
    }
121
122
    /**
123
     * Set tags on a response
124
     *
125
     * @param ResponseInterface $response Original response
126
     * @param bool              $replace  Whether to replace the current tags
127
     *                                    on the response
128
     *
129
     * @return ResponseInterface          Tagged response
130
     */
131 1
    public function tagResponse(ResponseInterface $response, $replace = false)
132
    {
133 1
        if ($replace) {
134 1
            return $response->withHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue());
135
        }
136
137
        return $response->withAddedHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue());
138
    }
139
}
140