Passed
Pull Request — 1.x (#125)
by
unknown
02:24
created

FastlyCachePurger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository\Cdn;
6
7
use BEAR\QueryRepository\PurgerInterface;
8
use Fastly\Api\PurgeApi;
9
use Fastly\ApiException;
10
use Ray\Di\Di\Named;
11
12
use function explode;
13
14
final class FastlyCachePurger implements PurgerInterface
15
{
16
    private PurgeApi $purgeApi;
17
    private string $fastlyServiceId;
18
    private bool $enableSoftPurge;
19
20
    /**
21
     * @SuppressWarnings("PHPMD.BooleanArgumentFlag")
22
     * @Named("fastlyServiceId=fastlyServiceId,enableSoftPurge=fastlyEnableSofrPurge")
23
     */
24
    public function __construct(
25
        PurgeApi $purgeApi,
26
        #[Named('fastlyServiceId')] string $fastlyServiceId,
27
        #[Named('fastlyEnableSoftPurge')] bool $enableSoftPurge
28
    ) {
29
        $this->purgeApi = $purgeApi;
30
        $this->fastlyServiceId = $fastlyServiceId;
31
        $this->enableSoftPurge = $enableSoftPurge;
32
    }
33
34
    /**
35
     * @throws ApiException
36
     *
37
     * @see https://developer.fastly.com/reference/api/purging/
38
     */
39
    public function __invoke(string $tag): void
40
    {
41
        if (empty($this->fastlyServiceId) || empty($tag)) {
42
            return;
43
        }
44
45
        $this->purgeApi->bulkPurgeTag([
46
            'fastly_soft_purge' => (int) $this->enableSoftPurge,
47
            'service_id' => $this->fastlyServiceId,
48
            'purge_response' => [
49
                'surrogate_keys' => explode(' ', $tag),
50
            ],
51
        ]);
52
    }
53
}
54