Passed
Pull Request — main (#155)
by Daniel
05:12
created

HttpCachePurger::collectIri()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\HttpCache;
15
16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\Exception\InvalidArgumentException;
20
use ApiPlatform\Exception\OperationNotFoundException;
21
use ApiPlatform\Exception\RuntimeException;
22
use ApiPlatform\HttpCache\PurgerInterface;
23
use ApiPlatform\Metadata\GetCollection;
24
use Doctrine\ORM\PersistentCollection;
25
26
class HttpCachePurger implements ResourceChangedPropagatorInterface
27
{
28
    private array $tags;
29
30
    public function __construct(
31
        private readonly IriConverterInterface $iriConverter,
32
        private readonly ResourceClassResolverInterface $resourceClassResolver,
33
        private readonly PurgerInterface $httpCachePurger,
34
    ) {
35
        $this->reset();
36
    }
37
38
    public function collectResource($entity, ?string $type = null): void
39
    {
40
        if (null === $entity) {
41
            return;
42
        }
43
44
        try {
45
            $resourceClass = $this->resourceClassResolver->getResourceClass($entity);
46
            $resourceIri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, (new GetCollection())->withClass($resourceClass));
47
            $this->collectIri([$resourceIri]);
48
        } catch (OperationNotFoundException|InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
49
        }
50
    }
51
52
    public function collectItems($items, ?string $type = null): void
53
    {
54
        if (!$items) {
55
            return;
56
        }
57
58
        if (!is_iterable($items)) {
59
            $this->collectItem($items);
60
61
            return;
62
        }
63
64
        if ($items instanceof PersistentCollection) {
65
            $items = clone $items;
66
        }
67
68
        foreach ($items as $i) {
69
            $this->collectItem($i);
70
        }
71
    }
72
73
    private function collectItem($item): void
74
    {
75
        try {
76
            $iri = $this->iriConverter->getIriFromResource($item);
77
            $this->collectIri([$iri]);
78
        } catch (InvalidArgumentException|RuntimeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
79
        }
80
    }
81
82
    private function collectIri(array $iris): void
83
    {
84
        foreach ($iris as $iri) {
85
            if (!\in_array($iri, $this->tags, true)) {
86
                $this->tags[$iri] = $iri;
87
            }
88
        }
89
    }
90
91
    public function propagate(): void
92
    {
93
        if (empty($this->tags)) {
94
            return;
95
        }
96
97
        $this->httpCachePurger->purge(array_values($this->tags));
98
        $this->reset();
99
    }
100
101
    public function reset(): void
102
    {
103
        $this->tags = [];
104
    }
105
}
106