Completed
Push — master ( 8ed3bd...5a8135 )
by Kévin
03:59
created

src/HttpCache/VarnishPurger.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 GuzzleHttp\ClientInterface;
17
18
/**
19
 * Purges Varnish.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 *
23
 * @experimental
24
 */
25
final class VarnishPurger implements PurgerInterface
26
{
27
    private $clients;
28
29
    /**
30
     * @param ClientInterface[] $clients
31
     */
32
    public function __construct(array $clients)
33
    {
34
        $this->clients = $clients;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function purge(array $iris)
41
    {
42
        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...
43
            return;
44
        }
45
46
        // Create the regex to purge all tags in just one request
47
        $parts = array_map(function ($iri) {
48
            return sprintf('(^|\,)%s($|\,)', preg_quote($iri));
49
        }, $iris);
50
51
        $regex = isset($parts[1]) ? sprintf('(%s)', implode(')|(', $parts)) : $parts[0];
52
53
        foreach ($this->clients as $client) {
54
            $client->requestAsync('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => $regex]]);
55
        }
56
    }
57
}
58