Completed
Push — master ( f4d77b...75a886 )
by Richan
13:04
created

InvalidateVarnishCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 104
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 6 1
A banByPatterns() 0 13 3
A banByUrls() 0 13 3
A getVarnishUrl() 0 4 1
A sendBanRequest() 0 10 2
getConfig() 0 1 ?
getGuzzle() 0 1 ?
1
<?php
2
3
namespace RichanFongdasen\Varnishable\Concerns;
4
5
trait InvalidateVarnishCache
6
{
7
    /**
8
     * Flush entire cache for an application hostname.
9
     *
10
     * @param  string $hostname [description]
11
     * @return void
12
     */
13
    public function flush($hostname)
14
    {
15
        $this->sendBanRequest([
16
            'X-Ban-Host' => $hostname
17
        ], 'FULLBAN');
18
    }
19
20
    /**
21
     * Generate ban request for an application hostname,
22
     * specified by a set of regular expression.
23
     *
24
     * @param  string       $hostname
25
     * @param  string|array $patterns
26
     * @return void
27
     */
28
    public function banByPatterns($hostname, $patterns)
29
    {
30
        if (!is_array($patterns)) {
31
            return $this->sendBanRequest([
32
                'X-Ban-Host' => $hostname,
33
                'X-Ban-Regex' => $patterns
34
            ]);
35
        }
36
37
        foreach ($patterns as $pattern) {
38
            $this->banByPatterns($hostname, $pattern);
39
        }
40
    }
41
42
    /**
43
     * Generate ban request for an application hostname,
44
     * specified by a set of urls.
45
     *
46
     * @param  string       $hostname
47
     * @param  string|array $urls
48
     * @return void
49
     */
50
    public function banByUrls($hostname, $urls)
51
    {
52
        if (!is_array($urls)) {
53
            return $this->sendBanRequest([
54
                'X-Ban-Host' => $hostname,
55
                'X-Ban-Url' => $urls
56
            ]);
57
        }
58
59
        foreach ($urls as $url) {
60
            $this->banByUrls($hostname, $url);
61
        }
62
    }
63
64
    /**
65
     * Get a valid varnish host url to send the 
66
     * ban request.
67
     *
68
     * @param  string $varnishHost
69
     * @return string
70
     */
71
    protected function getVarnishUrl($varnishHost)
72
    {
73
        return 'http://' . $varnishHost . ':' . $this->getConfig('varnish_port') . '/';
74
    }
75
76
    /**
77
     * Send the ban request to every varnish hosts.
78
     *
79
     * @param  array  $headers
80
     * @param  string $method
81
     * @return void
82
     */
83
    protected function sendBanRequest(array $headers, $method = 'BAN')
84
    {
85
        $guzzle = $this->getGuzzle();
86
87
        foreach ((array) $this->getConfig('varnish_hosts') as $varnishHost) {
88
            $url = $this->getVarnishUrl($varnishHost);
89
90
            $guzzle->request($method, $url, ['headers' => $headers]);
91
        }
92
    }
93
94
    /**
95
     * Get configuration value for a specific key.
96
     *
97
     * @param  string $key
98
     * @return mixed
99
     */
100
    abstract public function getConfig($key);
101
102
    /**
103
     * Get guzzle client object.
104
     *
105
     * @return \GuzzleHttp\Client
106
     */
107
    abstract public function getGuzzle();
108
}
109