Completed
Pull Request — master (#342)
by David de
11:31
created

MultiplexerClient::flush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 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\ProxyClient;
13
14
use FOS\HttpCache\Exception\ExceptionCollection;
15
use FOS\HttpCache\Exception\InvalidArgumentException;
16
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable;
17
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
18
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
19
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
20
21
/**
22
 * This class forwards invalidation to all attached clients.
23
 *
24
 * @author Emanuele Panzeri <[email protected]>
25
 */
26
class MultiplexerClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable
27
{
28
    /**
29
     * @var ProxyClient[]
30
     */
31
    private $proxyClients;
32
33
    /**
34
     * MultiplexerClient constructor.
35
     *
36
     * @param ProxyClient[] $proxyClients The list of Proxy clients
37 7
     */
38
    public function __construct(array $proxyClients)
39 7
    {
40 7
        foreach ($proxyClients as $proxyClient) {
41 2
            if (!$proxyClient instanceof ProxyClient) {
42 7
                throw new InvalidArgumentException(
43
                    'Expected ProxyClientInterface, got: '.
44
                    (is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient))
45
                );
46
            }
47 5
        }
48 5
49
        $this->proxyClients = $proxyClients;
50
    }
51
52
    /**
53
     * Forwards to all clients.
54
     *
55
     * @param array $headers HTTP headers that path must match to be banned
56
     *
57 1
     * @return $this
58
     */
59 1
    public function ban(array $headers)
60
    {
61 1
        $this->invoke(BanCapable::class, 'ban', [$headers]);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Forwards to all clients.
68
     *
69
     * @param string       $path        Regular expression pattern for URI to invalidate
70
     * @param string       $contentType Regular expression pattern for the content type to limit banning, for instance
71
     *                                  'text'
72
     * @param array|string $hosts       Regular expression of a host name or list of exact host names to limit banning
73
     *
74 1
     * @return $this
75
     */
76 1
    public function banPath($path, $contentType = null, $hosts = null)
77
    {
78 1
        $this->invoke(BanCapable::class, 'banPath', [$path, $contentType, $hosts]);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Forwards to all clients.
85
     *
86
     * @throws ExceptionCollection If any errors occurred during flush
87
     *
88 1
     * @return int The number of cache invalidations performed per caching server
89
     */
90 1
    public function flush()
91 1
    {
92 1
        $count = 0;
93
        foreach ($this->proxyClients as $proxyClient) {
94
            $count += $proxyClient->flush();
95 1
        }
96
97
        return $count;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getTagsHeaderValue(array $tags)
104
    {
105
        return $this->invokeFirst(TagCapable::class, 'getTagsHeaderValue', [$tags]);
106 1
    }
107
108 1
    /**
109
     * {@inheritdoc}
110 1
     */
111
    public function getTagsHeaderName()
112
    {
113
        return $this->invokeFirst(TagCapable::class, 'getTagsHeaderName', []);
114
    }
115
116
    /**
117
     * Forwards tag invalidation request to all clients.
118
     *
119
     * {@inheritdoc}
120
     *
121 1
     * @param array $tags
122
     *
123 1
     * @return $this
124
     */
125 1
    public function invalidateTags(array $tags)
126
    {
127
        $this->invoke(TagCapable::class, 'invalidateTags', [$tags]);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Forwards to all clients.
134
     *
135 4
     * @param string $url     Path or URL to purge
136
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
137 4
     *
138 4
     * @return $this
139 4
     */
140
    public function purge($url, array $headers = array())
141
    {
142 4
        $this->invoke(PurgeCapable::class, 'purge', [$url, $headers]);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Forwards to all clients.
149
     *
150
     * @param string $url     Path or URL to refresh
151
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
152
     *
153
     * @return $this
154
     */
155
    public function refresh($url, array $headers = [])
156
    {
157
        $this->invoke(RefreshCapable::class, 'refresh', [$url, $headers]);
158
159
        return $this;
160
    }
161
162
    /**
163
     * Invoke the given $method on all available ProxyClients implementing the
164
     * given $interface.
165
     *
166
     * @param string $interface The FQN of the interface
167
     * @param string $method    The method to invoke
168
     * @param array  $arguments The arguments to be passed to the method
169
     */
170
    private function invoke($interface, $method, array $arguments)
171
    {
172
        foreach ($this->getProxyClients($interface) as $proxyClient) {
173
            call_user_func_array([$proxyClient, $method], $arguments);
174
        }
175
    }
176
177
    /**
178
     * Invoke the given $method on the first available ProxyClient implementing
179
     * the given $interface.
180
     *
181
     * @param string $interface The FQN of the interface
182
     * @param string $method    The method to invoke
183
     * @param array  $arguments The arguments to be passed to the method
184
     *
185
     * @return mixed Return value of ProxyClient method call
186
     */
187
    private function invokeFirst($interface, $method, array $arguments)
188
    {
189
        foreach ($this->getProxyClients($interface) as $proxyClient) {
190
            return call_user_func_array([$proxyClient, $method], $arguments);
191
        }
192
    }
193
194
    /**
195
     * Get proxy clients that implement a feature interface
196
     *
197
     * @param string $interface
198
     *
199
     * @return ProxyClient[]
200
     */
201
    private function getProxyClients($interface)
202
    {
203
        return array_filter(
204
            $this->proxyClients,
205
            function ($proxyClient) use ($interface) {
206
                return is_subclass_of($proxyClient, $interface);
207
            }
208
        );
209
    }
210
}
211