Completed
Pull Request — master (#444)
by Yanick
03:08
created

LiteSpeed   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 8 1
A purge() 0 10 2
A invalidateTags() 0 8 1
A queuePurgeEndpointRequest() 0 7 1
A refresh() 0 7 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\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->queuePurgeEndpointRequest([
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->queueRequest('PURGE', $url, $headers);
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function invalidateTags(array $tags)
56
    {
57
        $this->queuePurgeEndpointRequest([
58
            'X-LiteSpeed-Purge' => implode(', ', preg_filter('/^/', 'tag=', $tags)),
59
        ]);
60
61
        return $this;
62
    }
63
64
    private function queuePurgeEndpointRequest(array $headers)
65
    {
66
        // TODO: Make this configurable
67
        $purgeEndpoint = '/_fos_litespeed_purge_endpoint/';
68
69
        $this->queueRequest('PURGE', $purgeEndpoint, $headers);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function refresh($url, array $headers = [])
76
    {
77
        $this->purge($url);
78
        $this->queueRequest('GET', $url, $headers);
79
80
        return $this;
81
    }
82
}
83