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

LiteSpeed   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 8 1
A purge() 0 12 2
A invalidateTags() 0 8 1
A refresh() 0 7 1
A queuePurgeRequest() 0 8 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->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
        $headers['Authorization'] = 'Basic Zm9zOmZvczEyMw=='; // fos:fos123
71
72
        $this->queueRequest('PURGE', $purgeEndpoint, $headers);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function refresh($url, array $headers = [])
79
    {
80
        $this->purge($url);
81
        $this->queueRequest('GET', $url, $headers);
82
83
        return $this;
84
    }
85
}
86