InvalidateVarnishCache::getGuzzle()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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