Completed
Push — master ( 1d2d2b...3bec43 )
by David
22:11 queued 15:59
created

MultiplexerClient::invokeFirst()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
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
     */
38
    public function __construct(array $proxyClients)
39 8
    {
40
        foreach ($proxyClients as $proxyClient) {
41 8
            if (!$proxyClient instanceof ProxyClient) {
42 8
                throw new InvalidArgumentException(
43 2
                    'Expected ProxyClientInterface, got: '.
44
                    (is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient))
45 8
                );
46
            }
47
        }
48
49
        $this->proxyClients = $proxyClients;
50 6
    }
51 6
52
    /**
53
     * Forwards to all clients.
54
     *
55
     * @param array $headers HTTP headers that path must match to be banned
56
     *
57
     * @return $this
58
     */
59
    public function ban(array $headers)
60 1
    {
61
        $this->invoke(BanCapable::class, 'ban', [$headers]);
62 1
63
        return $this;
64 1
    }
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
     * @return $this
75
     */
76
    public function banPath($path, $contentType = null, $hosts = null)
77 1
    {
78
        $this->invoke(BanCapable::class, 'banPath', [$path, $contentType, $hosts]);
79 1
80
        return $this;
81 1
    }
82
83
    /**
84
     * Forwards to all clients.
85
     *
86
     * @throws ExceptionCollection If any errors occurred during flush
87
     *
88
     * @return int The number of cache invalidations performed per caching server
89
     */
90
    public function flush()
91 1
    {
92
        $count = 0;
93 1
        foreach ($this->proxyClients as $proxyClient) {
94 1
            $count += $proxyClient->flush();
95 1
        }
96
97
        return $count;
98 1
    }
99
100
    /**
101
     * Forwards tag invalidation request to all clients.
102
     *
103
     * {@inheritdoc}
104
     *
105
     * @param array $tags
106
     *
107
     * @return $this
108
     */
109
    public function invalidateTags(array $tags)
110 1
    {
111
        $this->invoke(TagCapable::class, 'invalidateTags', [$tags]);
112 1
113
        return $this;
114 1
    }
115
116
    /**
117
     * Forwards to all clients.
118
     *
119
     * @param string $url     Path or URL to purge
120
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
121
     *
122
     * @return $this
123
     */
124
    public function purge($url, array $headers = [])
125 1
    {
126
        $this->invoke(PurgeCapable::class, 'purge', [$url, $headers]);
127 1
128
        return $this;
129 1
    }
130
131
    /**
132
     * Forwards to all clients.
133
     *
134
     * @param string $url     Path or URL to refresh
135
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
136
     *
137
     * @return $this
138
     */
139
    public function refresh($url, array $headers = [])
140 1
    {
141
        $this->invoke(RefreshCapable::class, 'refresh', [$url, $headers]);
142 1
143
        return $this;
144 1
    }
145
146
    /**
147
     * Invoke the given $method on all available ProxyClients implementing the
148
     * given $interface.
149
     *
150
     * @param string $interface The FQN of the interface
151
     * @param string $method    The method to invoke
152
     * @param array  $arguments The arguments to be passed to the method
153
     */
154
    private function invoke($interface, $method, array $arguments)
155 5
    {
156
        foreach ($this->getProxyClients($interface) as $proxyClient) {
157 5
            call_user_func_array([$proxyClient, $method], $arguments);
158 5
        }
159
    }
160 5
161
    /**
162
     * Get proxy clients that implement a feature interface.
163
     *
164
     * @param string $interface
165
     *
166
     * @return ProxyClient[]
167
     */
168
    private function getProxyClients($interface)
169
    {
170
        return array_filter(
171
            $this->proxyClients,
172
            function ($proxyClient) use ($interface) {
173
                return is_subclass_of($proxyClient, $interface);
174
            }
175
        );
176
    }
177
}
178