Completed
Pull Request — master (#451)
by André
02:15
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\RefreshCapable;
17
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Fastly HTTP cache invalidator.
22
 *
23
 * @author Simone Fumagalli <[email protected]>
24
 */
25
class Fastly extends HttpProxyClient implements ClearCapable, PurgeCapable, RefreshCapable, TagCapable
26
{
27
    /**
28
     * @internal
29
     */
30
    const HTTP_METHOD_PURGE = 'PURGE';
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @see https://docs.fastly.com/api/purge#purge_db35b293f8a724717fcf25628d713583
36
     */
37
    public function invalidateTags(array $tags)
38
    {
39
        $headers = [
40
            'Fastly-Key' => $this->options['authentication_token'],
41
            'Accept' => 'application/json',
42
        ];
43
44
        if (true === $this->options['soft_purge']) {
45
            $headers['Fastly-Soft-Purge'] = 1;
46
        }
47
48
        foreach (\array_chunk($tags, 256) as $tagChunk) {
49
            $this->queueRequest(
50
                Request::METHOD_POST,
51
                sprintf('/service/%s/purge', $this->options['service_identifier']),
52
                $headers + [
53
                    // Can be changed to use json payload if queueRequest is changed to expose possibility to pass body
54
                    'Surrogate-Key' => implode(' ', $tagChunk),
55
                ],
56
                false
57
            );
58
        }
59
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @see https://docs.fastly.com/api/purge#soft_purge_0c4f56f3d68e9bed44fb8b638b78ea36
67
     * @see https://docs.fastly.com/guides/purging/authenticating-api-purge-requests#purging-urls-with-an-api-token
68
     */
69
    public function purge($url, array $headers = [])
70
    {
71
        $headers['Fastly-Key'] = $this->options['authentication_token'];
72
73
        if (true === $this->options['soft_purge']) {
74
            $headers['Fastly-Soft-Purge'] = 1;
75
        }
76
77
        $this->queueRequest(
78
            self::HTTP_METHOD_PURGE,
79
            $url,
80
            $headers,
81
            false
82
        );
83
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function refresh($url, array $headers = [])
91
    {
92
        $headers['Fastly-Key'] = $this->options['authentication_token'];
93
94
        // First soft purge url
95
        $this->queueRequest(
96
            self::HTTP_METHOD_PURGE,
97
            $url,
98
            array_merge($headers, ['Fastly-Soft-Purge' => 1]),
99
            false
100
        );
101
102
        // Secondly make sure refresh is triggered
103
        $this->queueRequest(
104
            Request::METHOD_HEAD,
105
            $url,
106
            $headers,
107
            false
108
        );
109
110
        return $this;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     *
116
     * @see https://docs.fastly.com/api/purge#purge_bee5ed1a0cfd541e8b9f970a44718546
117
     *
118
     * Warning:
119
     * - Does not support soft purge, for that use an "all" key.
120
     * - Requires a API token of a user with at least Engineer permissions.
121
     */
122
    public function clear()
123
    {
124
        $headers = [
125
            'Fastly-Key' => $this->options['authentication_token'],
126
            'Accept' => 'application/json',
127
        ];
128
129
        $this->queueRequest(
130
            Request::METHOD_POST,
131
            sprintf('/service/%s/purge_all', $this->options['service_identifier']),
132
            $headers,
133
            false
134
        );
135
136
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    protected function configureOptions()
143
    {
144
        $resolver = parent::configureOptions();
145
146
        $resolver->setRequired([
147
            'authentication_token',
148
            'service_identifier',
149
            'soft_purge',
150
        ]);
151
152
        $resolver->setDefaults([
153
            'soft_purge' => true,
154
        ]);
155
156
        return $resolver;
157
    }
158
}
159