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

FastlyCachePurger::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
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=FASTLY_SERVICE_ID,enableSoftPurge=FASTLY_ENABLE_SOFT_PURGE")
23
     */
24
    public function __construct(
25
        PurgeApi $purgeApi,
26
        #[Named('FASTLY_SERVICE_ID')] string $fastlyServiceId,
27
        #[Named('FASTLY_ENABLE_SOFT_PURGE')] bool $enableSoftPurge
28
    ) {
29
        $this->purgeApi = $purgeApi;
30
        $this->fastlyServiceId = $fastlyServiceId;
31
        $this->enableSoftPurge = $enableSoftPurge;
32
    }
33
34
    /**
35
     * @throws ApiException
36
     */
37
    public function __invoke(string $tag): void
38
    {
39
        if (empty($this->fastlyServiceId) || empty($tag)) {
40
            return;
41
        }
42
43
        $this->purgeApi->bulkPurgeTag([
44
            'fastly_soft_purge' => (int) $this->enableSoftPurge,
45
            'service_id' => $this->fastlyServiceId,
46
            'purge_response' => [
47
                'surrogate_keys' => explode(' ', $tag),
48
            ],
49
        ]);
50
    }
51
}
52