Completed
Pull Request — master (#342)
by David de
16:11 queued 14:11
created

MultiplexerClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.56%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 152
ccs 29
cts 36
cp 0.8056
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A ban() 0 6 1
A banPath() 0 6 1
A flush() 0 9 2
A getTagsHeaderValue() 0 4 1
A getTagsHeaderName() 0 4 1
A invalidateTags() 0 6 1
A purge() 0 6 1
A refresh() 0 6 1
A invoke() 0 8 3
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 7
    public function __construct(array $proxyClients)
39
    {
40 7
        foreach ($proxyClients as $proxyClient) {
41 7
            if (!$proxyClient instanceof ProxyClient) {
42 2
                throw new InvalidArgumentException(
43
                    'Expected ProxyClientInterface, got: '.
44 7
                    (is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient))
45
                );
46
            }
47
        }
48
49 5
        $this->proxyClients = $proxyClients;
50 5
    }
51
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 1
    public function ban(array $headers)
60
    {
61 1
        $this->invoke(BanCapable::class, 'ban', [$headers]);
62
63 1
        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
     * @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 1
        return $this;
81
    }
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 1
    public function flush()
91
    {
92 1
        $count = 0;
93 1
        foreach ($this->proxyClients as $proxyClient) {
94 1
            $count += $proxyClient->flush();
95
        }
96
97 1
        return $count;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getTagsHeaderValue(array $tags)
104
    {
105
        // TODO
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getTagsHeaderName()
112
    {
113
        // TODO
114
    }
115
116
    /**
117
     * Forwards tag invalidation request to all clients.
118
     *
119
     * {@inheritdoc}
120
     *
121
     * @param array $tags
122
     *
123
     * @return $this
124
     */
125
    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
     * @param string $url     Path or URL to purge
136
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
137
     *
138
     * @return $this
139
     */
140 1
    public function purge($url, array $headers = array())
141
    {
142 1
        $this->invoke(PurgeCapable::class, 'purge', [$url, $headers]);
143
144 1
        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 1
    public function refresh($url, array $headers = [])
156
    {
157 1
        $this->invoke(RefreshCapable::class, 'refresh', [$url, $headers]);
158
159 1
        return $this;
160
    }
161
162
    /**
163
     * Helper function to invoke the given $method on all available ProxyClients implementing the given $interface.
164
     *
165
     * @param string $interface The FQN of the interface
166
     * @param string $method    The method to invoke
167
     * @param array  $arguments The arguments to be passed to the method
168
     */
169 4
    private function invoke($interface, $method, array $arguments)
170
    {
171 4
        foreach ($this->proxyClients as $proxyClient) {
172 4
            if (is_subclass_of($proxyClient, $interface)) {
173 4
                call_user_func_array([$proxyClient, $method], $arguments);
174
            }
175
        }
176 4
    }
177
}
178