Passed
Pull Request — 1.x (#125)
by
unknown
03:25 queued 01:06
created

FastlyCachePurger   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 6 1
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
        $this->purgeApi->bulkPurgeTag([
42
            'fastly_soft_purge' => (int) $this->enableSoftPurge,
43
            'service_id' => $this->fastlyServiceId,
44
            'purge_response' => ['surrogate_keys' => explode(' ', $tag)]
45
        ]);
46
    }
47
}
48