Completed
Pull Request — master (#952)
by Kévin
08:17
created

VarnishPurger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A purge() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * This file is part of the API Platform project.
6
 *
7
 * (c) Kévin Dunglas <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace ApiPlatform\Core\HttpCache;
14
15
use GuzzleHttp\ClientInterface;
16
17
/**
18
 * Purges Varnish.
19
 *
20
 * @author Kévin Dunglas <[email protected]>
21
 *
22
 * @experimental
23
 */
24
final class VarnishPurger implements PurgerInterface
25
{
26
    private $clients;
27
28
    /**
29
     * @param ClientInterface[] $clients
30
     */
31
    public function __construct(array $clients)
32
    {
33
        $this->clients = $clients;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function purge(array $iris)
40
    {
41
        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...
42
            return;
43
        }
44
45
        // Create the regex to purge all tags in just one request
46
        $parts = array_map(function ($iri) {
47
            sprintf('(^|\,)%s($|\,)', preg_quote($iri));
48
        }, $iris);
49
        $regex = isset($parts[1]) ? sprintf('(%s)', implode(')|(', $parts)) : $parts[0];
50
51
        foreach ($this->clients as $client) {
52
            $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...
53
        }
54
    }
55
}
56