Completed
Pull Request — master (#451)
by André
02:37
created

Fastly::configureOptions()   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\PurgeCapable;
15
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Fastly HTTP cache invalidator.
20
 *
21
 * @author Simone Fumagalli <[email protected]>
22
 */
23
class Fastly extends HttpProxyClient implements TagCapable, PurgeCapable
24
{
25
    /**
26
     * @internal
27
     */
28
    const HTTP_METHOD_PURGE = 'PURGE';
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @see https://docs.fastly.com/api/purge#purge_db35b293f8a724717fcf25628d713583
34
     */
35
    public function invalidateTags(array $tags)
36
    {
37
        $headers = [
38
            'Fastly-Key' => $this->options['authentication_token'],
39
            'Accept' => 'application/json'
40
        ];
41
42
        if (true === $this->options['soft_purge']) {
43
            $headers['Fastly-Soft-Purge'] = 1;
44
        }
45
46
        foreach (\array_chunk($tags, 256) as $tagchunk) {
47
            $this->queueRequest(
48
                Request::METHOD_POST,
49
                sprintf("/service/%s/purge", $this->options['service_identifier']),
50
                $headers + [
51
                    'Surrogate-Key' => implode(' ', $tagchunk),
52
                ],
53
                false
54
            );
55
        }
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @see https://docs.fastly.com/api/purge#soft_purge_0c4f56f3d68e9bed44fb8b638b78ea36
64
     * @see https://docs.fastly.com/guides/purging/authenticating-api-purge-requests#purging-urls-with-an-api-token
65
     */
66
    public function purge($url, array $headers = [])
67
    {
68
        $headers['Fastly-Key'] = $this->options['authentication_token'];
69
        if (true === $this->options['soft_purge']) {
70
            $headers['Fastly-Soft-Purge'] = 1;
71
        }
72
73
        $this->queueRequest(
74
            self::HTTP_METHOD_PURGE,
75
            $url,
76
            $headers,
77
            false
78
        );
79
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function configureOptions()
87
    {
88
        $resolver = parent::configureOptions();
89
90
        $resolver->setRequired([
91
            'authentication_token',
92
            'service_identifier',
93
            'soft_purge'
94
        ]);
95
96
        $resolver->setDefaults([
97
            'soft_purge' => true,
98
        ]);
99
100
        return $resolver;
101
    }
102
103
}
104