Completed
Push — deleted_model_event_bug ( f45daa )
by Richan
23:54
created

InvalidateVarnishCache::banByPatterns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace RichanFongdasen\Varnishable\Concerns;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\GuzzleException;
7
use InvalidArgumentException;
8
9
trait InvalidateVarnishCache
10
{
11
    /**
12
     * Flush entire cache for an application hostname.
13
     *
14
     * @param string $hostname
15
     *
16
     * @return void
17
     *
18
     * @throws GuzzleException
19
     * @throws InvalidArgumentException
20
     */
21
    public function flush(string $hostname) :void
22
    {
23
        $this->sendBanRequest([
24
            'X-Ban-Host' => $hostname,
25
        ], 'FULLBAN');
26
    }
27
28
    /**
29
     * Generate ban request for an application hostname,
30
     * specified by a regular expression.
31
     *
32
     * @param string $hostname
33
     * @param string $pattern
34
     *
35
     * @return void
36
     *
37
     * @throws GuzzleException
38
     * @throws InvalidArgumentException
39
     */
40
    public function banByPattern(string $hostname, string $pattern) :void
41
    {
42
        $this->sendBanRequest([
43
            'X-Ban-Host'  => $hostname,
44
            'X-Ban-Regex' => $pattern,
45
        ]);
46
    }
47
48
    /**
49
     * Generate ban request for an application hostname,
50
     * specified by a set of regular expression.
51
     *
52
     * @param string $hostname
53
     * @param array  $patterns
54
     *
55
     * @return void
56
     *
57
     * @throws GuzzleException
58
     * @throws InvalidArgumentException
59
     */
60
    public function banByPatterns(string $hostname, array $patterns) :void
61
    {
62
        foreach ($patterns as $pattern) {
63
            $this->banByPattern($hostname, $pattern);
64
        }
65
    }
66
67
    /**
68
     * Generate ban request for an application hostname,
69
     * specified by a single urls.
70
     *
71
     * @param string $hostname
72
     * @param string $url
73
     *
74
     * @return void
75
     *
76
     * @throws GuzzleException
77
     * @throws InvalidArgumentException
78
     */
79
    public function banByUrl(string $hostname, string $url) :void
80
    {
81
        $this->sendBanRequest([
82
            'X-Ban-Host' => $hostname,
83
            'X-Ban-Url'  => $url,
84
        ]);
85
    }
86
87
    /**
88
     * Generate ban request for an application hostname,
89
     * specified by a set of urls.
90
     *
91
     * @param string $hostname
92
     * @param array  $urls
93
     *
94
     * @return void
95
     *
96
     * @throws GuzzleException
97
     * @throws InvalidArgumentException
98
     */
99
    public function banByUrls(string $hostname, array $urls) :void
100
    {
101
        foreach ($urls as $url) {
102
            $this->banByUrl($hostname, $url);
103
        }
104
    }
105
106
    /**
107
     * Get a valid varnish host url to send the
108
     * ban request.
109
     *
110
     * @param string $varnishHost
111
     *
112
     * @return string
113
     */
114
    protected function getVarnishUrl(string $varnishHost) :string
115
    {
116
        return 'http://'.$varnishHost.':'.$this->getConfig('varnish_port').'/';
117
    }
118
119
    /**
120
     * Send the ban request to every varnish hosts.
121
     *
122
     * @param array  $headers
123
     * @param string $method
124
     *
125
     * @return void
126
     *
127
     * @throws GuzzleException
128
     * @throws InvalidArgumentException
129
     */
130
    protected function sendBanRequest(array $headers, string $method = 'BAN') :void
131
    {
132
        $guzzle = $this->getGuzzle();
133
134
        foreach ((array) $this->getConfig('varnish_hosts') as $varnishHost) {
135
            $url = $this->getVarnishUrl($varnishHost);
136
137
            $guzzle->request($method, $url, ['headers' => $headers]);
138
        }
139
    }
140
141
    /**
142
     * Get configuration value for a specific key.
143
     *
144
     * @param string $key
145
     *
146
     * @return mixed
147
     */
148
    abstract public function getConfig($key);
149
150
    /**
151
     * Get guzzle client object.
152
     *
153
     * @return \GuzzleHttp\Client
154
     */
155
    abstract public function getGuzzle() :Client;
156
}
157