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

VarnishPurger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A purge() 0 15 4
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
namespace ApiPlatform\Core\HttpCache;
13
14
use GuzzleHttp\ClientInterface;
15
16
/**
17
 * Purges Varnish.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 *
21
 * @experimental
22
 */
23
final class VarnishPurger implements PurgerInterface
24
{
25
    private $clients;
26
27
    /**
28
     * @param ClientInterface[] $clients
29
     */
30
    public function __construct(array $clients)
31
    {
32
        $this->clients = $clients;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function purge(array $iris)
39
    {
40
        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...
41
            return;
42
        }
43
44
        $parts = array_map(function ($iri) {
45
            sprintf('(^|\,)%s($|\,)', preg_quote($iri));
46
        }, $iris);
47
        $regex = isset($parts[1]) ? sprintf('(%s)', implode(')|(', $parts)) : $parts[0];
48
49
        foreach ($this->clients as $client) {
50
            $client->sendAsync($client->request('BAN', '', ['headers' => ['X-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...
51
        }
52
    }
53
}
54