Completed
Pull Request — master (#451)
by André
22:08 queued 07:08
created

Fastly::purge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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\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
                    // TODO: change to use json payload if queueRequest is changed to expose possibility to pass body
53
                    'Surrogate-Key' => implode(' ', $tagChunk),
54
                ],
55
                false
56
            );
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @see https://docs.fastly.com/api/purge#soft_purge_0c4f56f3d68e9bed44fb8b638b78ea36
66
     * @see https://docs.fastly.com/guides/purging/authenticating-api-purge-requests#purging-urls-with-an-api-token
67
     */
68
    public function purge($url, array $headers = [])
69
    {
70
        $headers['Fastly-Key'] = $this->options['authentication_token'];
71
72
        if (true === $this->options['soft_purge']) {
73
            $headers['Fastly-Soft-Purge'] = 1;
74
        }
75
76
        $this->queueRequest(
77
            self::HTTP_METHOD_PURGE,
78
            $url,
79
            $headers,
80
            false
81
        );
82
83
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     *
89
     * @see https://docs.fastly.com/api/purge#purge_bee5ed1a0cfd541e8b9f970a44718546
90
     *
91
     * Warning:
92
     * - Does not support soft purge, for that use an "all" key.
93
     * - Requires a API token of a user with at least Engineer permissions.
94
     */
95
    public function clear()
96
    {
97
        $headers = [
98
            'Fastly-Key' => $this->options['authentication_token'],
99
            'Accept' => 'application/json',
100
        ];
101
102
        $this->queueRequest(
103
            Request::METHOD_POST,
104
            sprintf('/service/%s/purge_all', $this->options['service_identifier']),
105
            $headers,
106
            false
107
        );
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    protected function configureOptions()
116
    {
117
        $resolver = parent::configureOptions();
118
119
        $resolver->setRequired([
120
            'authentication_token',
121
            'service_identifier',
122
            'soft_purge',
123
        ]);
124
125
        $resolver->setDefaults([
126
            'soft_purge' => true,
127
        ]);
128
129
        return $resolver;
130
    }
131
}
132