Completed
Pull Request — master (#289)
by Gavin
16:24 queued 13:47
created

Nginx   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 1
cbo 1
dl 0
loc 71
ccs 0
cts 30
cp 0
rs 10

4 Methods

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