Completed
Pull Request — master (#952)
by Kévin
03:20
created

VarnishPurger::purge()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
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 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
            sprintf('(^|\,)%s($|\,)', preg_quote($iri));
49
        }, $iris);
50
        $regex = isset($parts[1]) ? sprintf('(%s)', implode(')|(', $parts)) : $parts[0];
51
52
        foreach ($this->clients as $client) {
53
            $client->sendAsync($client->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => $regex]]));
0 ignored issues
show
Documentation introduced by
$client->request('BAN', ...Ban-Regex' => $regex))) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Psr\Http\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        }
55
    }
56
}
57