Passed
Pull Request — master (#1689)
by Yanick
03:18
created

SymfonyPurger::purge()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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 ApiPlatform\Core\HttpCache;
15
16
use Toflar\Psr6HttpCacheStore\Psr6StoreInterface;
17
18
/**
19
 * Purges Symfony's HttpCache.
20
 *
21
 * @author Yanick Witschi <[email protected]>
22
 */
23
final class SymfonyPurger implements PurgerInterface
24
{
25
    /**
26
     * @var StoreAwareKernelInterface
27
     */
28
    private $kernel;
29
30
    /**
31
     * @param StoreAwareKernelInterface $kernel
32
     */
33
    public function __construct(StoreAwareKernelInterface $kernel)
34
    {
35
        $this->kernel = $kernel;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function purge(array $iris)
42
    {
43
        if (!$iris) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $iris of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
44
            return;
45
        }
46
47
        $store = $this->kernel->getStore();
48
49
        if (!$store instanceof Psr6StoreInterface) {
50
            return;
51
        }
52
53
        // Encode tags for greater compatiblity with different proxies
54
        // Some do not allow special characters like / or @ in cache tags and
55
        // also it allows to use a , in a tag, if you wish to do so.
56
        $iris = array_map(function($resource) {
57
            return base64_encode($resource);
58
        }, $iris);
59
60
        $store->invalidateTags($iris);
61
    }
62
}
63