Completed
Pull Request — master (#451)
by André
03:38
created

Fastly::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\TagCapable;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Fastly HTTP cache invalidator.
21
 *
22
 * @author Simone Fumagalli <[email protected]>
23
 */
24
class Fastly extends HttpProxyClient implements TagCapable, PurgeCapable, ClearCapable
25
{
26
    /**
27
     * @internal
28
     */
29
    const HTTP_METHOD_PURGE = 'PURGE';
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @see https://docs.fastly.com/api/purge#purge_db35b293f8a724717fcf25628d713583
35
     */
36
    public function invalidateTags(array $tags)
37
    {
38
        $headers = [
39
            'Fastly-Key' => $this->options['authentication_token'],
40
            'Accept' => 'application/json'
41
        ];
42
43
        if (true === $this->options['soft_purge']) {
44
            $headers['Fastly-Soft-Purge'] = 1;
45
        }
46
47
        foreach (\array_chunk($tags, 256) as $tagchunk) {
48
            $this->queueRequest(
49
                Request::METHOD_POST,
50
                sprintf("/service/%s/purge", $this->options['service_identifier']),
51
                $headers + [
52
                    'Surrogate-Key' => implode(' ', $tagchunk),
53
                ],
54
                false
55
            );
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @see https://docs.fastly.com/api/purge#soft_purge_0c4f56f3d68e9bed44fb8b638b78ea36
65
     * @see https://docs.fastly.com/guides/purging/authenticating-api-purge-requests#purging-urls-with-an-api-token
66
     */
67
    public function purge($url, array $headers = [])
68
    {
69
        $headers['Fastly-Key'] = $this->options['authentication_token'];
70
        if (true === $this->options['soft_purge']) {
71
            $headers['Fastly-Soft-Purge'] = 1;
72
        }
73
74
        $this->queueRequest(
75
            self::HTTP_METHOD_PURGE,
76
            $url,
77
            $headers,
78
            false
79
        );
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @see https://docs.fastly.com/api/purge#purge_bee5ed1a0cfd541e8b9f970a44718546
88
     *
89
     * Warning:
90
     * - Does not support soft purge, for that use an "all" key.
91
     * - Requires a API token of a user with at least Engineer permissions.
92
     */
93
    public function clear()
94
    {
95
        $headers = [
96
            'Fastly-Key' => $this->options['authentication_token'],
97
            'Accept' => 'application/json'
98
        ];
99
100
        $this->queueRequest(
101
            Request::METHOD_POST,
102
            sprintf("/service/%s/purge_all", $this->options['service_identifier']),
103
            $headers,
104
            false
105
        );
106
107
        return $this;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function configureOptions()
114
    {
115
        $resolver = parent::configureOptions();
116
117
        $resolver->setRequired([
118
            'authentication_token',
119
            'service_identifier',
120
            'soft_purge'
121
        ]);
122
123
        $resolver->setDefaults([
124
            'soft_purge' => true,
125
        ]);
126
127
        return $resolver;
128
    }
129
130
}
131