Completed
Pull Request — master (#312)
by David
12:51 queued 09:06
created

Nginx::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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
 * 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 PurgeInterface, RefreshInterface
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 2
    public function refresh($url, array $headers = [])
36
    {
37 2
        $headers = array_merge($headers, [self::HTTP_HEADER_REFRESH => '1']);
38 2
        $this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers);
39
40 2
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 4
    public function purge($url, array $headers = [])
47
    {
48 4
        $purgeUrl = $this->buildPurgeUrl($url);
49 4
        $this->queueRequest(self::HTTP_METHOD_PURGE, $purgeUrl, $headers);
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    protected function getDefaultOptions()
58
    {
59 6
        $resolver = parent::getDefaultOptions();
60 6
        $resolver->setDefaults(['purge_location' => false]);
61
62 6
        return $resolver;
63
    }
64
65
    /**
66
     * Create the correct URL to purge a resource.
67
     *
68
     *
69
     * @param string $url URL
70
     *
71
     * @return string Rewritten URL
72
     */
73 4
    private function buildPurgeUrl($url)
74
    {
75 4
        if (empty($this->purgeLocation)) {
0 ignored issues
show
Bug introduced by
The property purgeLocation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76 4
            return $url;
77
        }
78
79
        $urlParts = parse_url($url);
80
81
        if (isset($urlParts['host'])) {
82
            $pathStartAt = strpos($url, $urlParts['path']);
83
            $purgeUrl = substr($url, 0, $pathStartAt).$this->purgeLocation.substr($url, $pathStartAt);
84
        } else {
85
            $purgeUrl = $this->purgeLocation.$url;
86
        }
87
88
        return $purgeUrl;
89
    }
90
}
91