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

MultiplexerClient::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\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
    public function __construct(array $proxyClients)
36
    {
37
        foreach ($proxyClients as $proxyClient) {
38
            if (!$proxyClient instanceof ProxyClientInterface) {
39
                throw new InvalidArgumentException('Expected ProxyClientInterface, got: '.
40
                    (is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient))
41
                );
42
            }
43
        }
44
45
        $this->proxyClients = $proxyClients;
46
    }
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
    public function ban(array $headers)
56
    {
57
        $this->invoke(BanInterface::class, 'ban', [$headers]);
58
59
        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
    public function banPath($path, $contentType = null, $hosts = null)
73
    {
74
        $this->invoke(BanInterface::class, 'banPath', [$path, $contentType, $hosts]);
75
76
        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
    public function flush()
87
    {
88
        $count = 0;
89
        foreach ($this->proxyClients as $proxyClient) {
90
            $count += $proxyClient->flush();
91
        }
92
93
        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
    public function refresh($url, array $headers = [])
120
    {
121
        $this->invoke(RefreshInterface::class, 'refresh', [$url, $headers]);
122
123
        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
    private function invoke($interface, $method, array $arguments)
134
    {
135
        foreach ($this->proxyClients as $proxyClient) {
136
            if (is_subclass_of($proxyClient, $interface)) {
137
                call_user_func_array([$proxyClient, $method], $arguments);
138
            }
139
        }
140
    }
141
}
142