Completed
Push — master ( a40985...862247 )
by Kévin
14s
created

FilterLocatorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setFilterLocator() 0 12 7
B getFilter() 0 10 5
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\Api;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use Psr\Container\ContainerInterface;
18
19
/**
20
 * Manipulates filters with a backward compatibility between the new filter locator and the deprecated filter collection.
21
 *
22
 * @author Baptiste Meyer <[email protected]>
23
 *
24
 * @internal
25
 */
26
trait FilterLocatorTrait
27
{
28
    private $filterLocator;
29
30
    /**
31
     * Sets a filter locator with a backward compatibility.
32
     *
33
     * @param ContainerInterface|FilterCollection|null $filterLocator
34
     * @param bool                                     $allowNull
35
     */
36
    private function setFilterLocator($filterLocator = null, bool $allowNull = false)
37
    {
38
        if ($filterLocator instanceof ContainerInterface || $filterLocator instanceof FilterCollection || null === $filterLocator && $allowNull) {
39
            if ($filterLocator instanceof FilterCollection) {
40
                @trigger_error(sprintf('The %s class is deprecated since version 2.1 and will be removed in 3.0. Provide an implementation of %s instead.', FilterCollection::class, ContainerInterface::class), E_USER_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
41
            }
42
43
            $this->filterLocator = $filterLocator;
44
        } else {
45
            throw new InvalidArgumentException(sprintf('The "$filterLocator" argument is expected to be an implementation of the "%s" interface%s.', ContainerInterface::class, $allowNull ? ' or null' : ''));
46
        }
47
    }
48
49
    /**
50
     * Gets a filter with a backward compatibility.
51
     *
52
     * @param string $filterId
53
     *
54
     * @return FilterInterface|null
55
     */
56
    private function getFilter(string $filterId)
57
    {
58
        if ($this->filterLocator instanceof ContainerInterface && $this->filterLocator->has($filterId)) {
59
            return $this->filterLocator->get($filterId);
60
        }
61
62
        if ($this->filterLocator instanceof FilterCollection && $this->filterLocator->offsetExists($filterId)) {
63
            return $this->filterLocator->offsetGet($filterId);
64
        }
65
    }
66
}
67