Completed
Pull Request — master (#364)
by Alessandro
04:31
created

Nginx   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 64
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 7 1
A purge() 0 7 1
A configureOptions() 0 7 1
A buildPurgeUrl() 0 17 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\ProxyClient\Invalidation\PurgeCapable;
15
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
16
17
/**
18
 * NGINX HTTP cache invalidator.
19
 *
20
 * Additional constructor options:
21
 * - purge_location: Path location that triggers purging. String, or set to
22
 *   boolean false for same location purging.
23
 *
24
 * @author Simone Fumagalli <[email protected]>
25
 */
26
class Nginx extends HttpProxyClient implements PurgeCapable, RefreshCapable
27
{
28
    const HTTP_METHOD_PURGE = 'PURGE';
29
    const HTTP_METHOD_REFRESH = 'GET';
30
    const HTTP_HEADER_REFRESH = 'X-Refresh';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function refresh($url, array $headers = [])
36
    {
37
        $headers = array_merge($headers, [self::HTTP_HEADER_REFRESH => '1']);
38
        $this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers);
39
40
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function purge($url, array $headers = [])
47
    {
48
        $purgeUrl = $this->buildPurgeUrl($url);
49
        $this->queueRequest(self::HTTP_METHOD_PURGE, $purgeUrl, $headers);
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function configureOptions()
58
    {
59
        $resolver = parent::configureOptions();
60
        $resolver->setDefaults(['purge_location' => false]);
61
62
        return $resolver;
63
    }
64
65
    /**
66
     * Create the correct URL to purge a resource.
67
     *
68
     * @param string $url URL
69
     *
70
     * @return string Rewritten URL
71
     */
72
    private function buildPurgeUrl($url)
73
    {
74
        if (!$this->options['purge_location']) {
75
            return $url;
76
        }
77
78
        $urlParts = parse_url($url);
79
80
        if (isset($urlParts['host'])) {
81
            $pathStartAt = strpos($url, $urlParts['path']);
82
            $purgeUrl = substr($url, 0, $pathStartAt).$this->options['purge_location'].substr($url, $pathStartAt);
83
        } else {
84
            $purgeUrl = $this->options['purge_location'].$url;
85
        }
86
87
        return $purgeUrl;
88
    }
89
}
90