Completed
Pull Request — master (#271)
by
unknown
13:29
created

ResponseTagger::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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