Completed
Pull Request — master (#444)
by Yanick
02:55
created

LiteSpeed::purge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\ClearCapable;
15
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
16
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
17
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
18
19
/**
20
 * LiteSpeed (OpenLiteSpeed or LiteSpeed Web Server) invalidator.
21
 *
22
 * @author Yanick Witschi <[email protected]>
23
 */
24
class LiteSpeed extends HttpProxyClient implements PurgeCapable, TagCapable, ClearCapable, RefreshCapable
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function clear()
30
    {
31
        $this->queuePurgeRequest([
32
            'X-LiteSpeed-Purge' => '*',
33
        ]);
34
35
        return $this;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function purge($url, array $headers = [])
42
    {
43
        // LiteSpeed only supports purging by relative URLs
44
        $urlParts = parse_url($url);
45
        $url = array_key_exists('path', $urlParts) ? $urlParts['path'] : '/';
46
47
        $this->queuePurgeRequest([
48
            'X-LiteSpeed-Purge' => $url,
49
        ]);
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function invalidateTags(array $tags)
58
    {
59
        $this->queuePurgeRequest([
60
            'X-LiteSpeed-Purge' => implode(', ', preg_filter('/^/', 'tag=', $tags)),
61
        ]);
62
63
        return $this;
64
    }
65
66
    private function queuePurgeRequest(array $headers)
67
    {
68
        // TODO: LiteSpeed is likely going to hard code this URL, otherwise it has to be configurable
69
        $purgeEndpoint = '/_fos_litespeed_purge_endpoint/';
70
71
        $this->queueRequest('PURGE', $purgeEndpoint, $headers);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function refresh($url, array $headers = [])
78
    {
79
        $this->purge($url);
80
        $this->queueRequest('GET', $url, $headers);
81
82
        return $this;
83
    }
84
}
85