Completed
Pull Request — master (#308)
by Ema
95:39 queued 86:07
created

MultiplexerClient   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.18%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 13
c 2
b 1
f 1
lcom 1
cbo 2
dl 0
loc 119
ccs 31
cts 34
cp 0.9118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A ban() 0 6 1
A banPath() 0 6 1
A flush() 0 9 2
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\BanInterface;
17
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
18
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
19
20
/**
21
 * This class forwards invalidation to all attached clients.
22
 */
23
class MultiplexerClient implements BanInterface, PurgeInterface, RefreshInterface
24
{
25
    /**
26
     * @var ProxyClientInterface[]
27
     */
28
    private $proxyClients;
29
30
    /**
31
     * MultiplexerClient constructor.
32
     *
33
     * @param ProxyClientInterface[] $proxyClients The list of Proxy clients
34
     */
35 6
    public function __construct(array $proxyClients)
36
    {
37 6
        foreach ($proxyClients as $proxyClient) {
38 6
            if (!$proxyClient instanceof ProxyClientInterface) {
39 2
                throw new InvalidArgumentException('Expected ProxyClientInterface, got: '.
40 2
                    (is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient))
41 2
                );
42
            }
43 4
        }
44
45 4
        $this->proxyClients = $proxyClients;
46 4
    }
47
48
    /**
49
     * Forwards to all clients.
50
     *
51
     * @param array $headers HTTP headers that path must match to be banned
52
     *
53
     * @return $this
54
     */
55 1
    public function ban(array $headers)
56
    {
57 1
        $this->invoke(BanInterface::class, 'ban', [$headers]);
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Forwards to all clients.
64
     *
65
     * @param string       $path        Regular expression pattern for URI to invalidate
66
     * @param string       $contentType Regular expression pattern for the content type to limit banning, for instance
67
     *                                  'text'
68
     * @param array|string $hosts       Regular expression of a host name or list of exact host names to limit banning
69
     *
70
     * @return $this
71
     */
72 1
    public function banPath($path, $contentType = null, $hosts = null)
73
    {
74 1
        $this->invoke(BanInterface::class, 'banPath', [$path, $contentType, $hosts]);
75
76 1
        return $this;
77
    }
78
79
    /**
80
     * Forwards to all clients.
81
     *
82
     * @return int The number of cache invalidations performed per caching server
83
     *
84
     * @throws ExceptionCollection If any errors occurred during flush
85
     */
86 1
    public function flush()
87
    {
88 1
        $count = 0;
89 1
        foreach ($this->proxyClients as $proxyClient) {
90 1
            $count += $proxyClient->flush();
91 1
        }
92
93 1
        return $count;
94
    }
95
96
    /**
97
     * Forwards to all clients.
98
     *
99
     * @param string $url     Path or URL to purge
100
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
101
     *
102
     * @return $this
103
     */
104
    public function purge($url, array $headers = array())
105
    {
106
        $this->invoke(PurgeInterface::class, 'purge', [$url, $headers]);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Forwards to all clients.
113
     *
114
     * @param string $url     Path or URL to refresh
115
     * @param array  $headers Extra HTTP headers to send to the caching proxy (optional)
116
     *
117
     * @return $this
118
     */
119 1
    public function refresh($url, array $headers = [])
120
    {
121 1
        $this->invoke(RefreshInterface::class, 'refresh', [$url, $headers]);
122
123 1
        return $this;
124
    }
125
126
    /**
127
     * Helper function to invoke the given $method on all available ProxyClients implementing the given $interface.
128
     *
129
     * @param string $interface The FQN of the interface
130
     * @param string $method    The method to invoke
131
     * @param array  $arguments The arguments to be passed to the method
132
     */
133 3
    private function invoke($interface, $method, array $arguments)
134
    {
135 3
        foreach ($this->proxyClients as $proxyClient) {
136 3
            if (is_subclass_of($proxyClient, $interface)) {
137 3
                call_user_func_array([$proxyClient, $method], $arguments);
138 3
            }
139 3
        }
140 3
    }
141
}
142