Passed
Push — master ( d1c5a6...bac8bf )
by Fabian
03:36 queued 12s
created

FilterManager::get()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 22
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle;
6
7
use Assetic\Contracts\Filter\FilterInterface;
8
use Assetic\FilterManager as AsseticFilterManager;
9
use Interop\Container\ContainerInterface;
10
use Fabiang\AsseticBundle\Exception\InvalidArgumentException;
11
12
class FilterManager extends AsseticFilterManager
13
{
14
15
    protected ContainerInterface $container;
16
17
    public function __construct(ContainerInterface $container)
18
    {
19
        $this->container = $container;
20
    }
21
22
    /**
23
     * @param mixed $alias
24
     */
25
    public function has($alias): bool
26
    {
27
        return parent::has($alias) ? true : $this->container->has($alias);
28
    }
29
30
    /**
31
     * @param mixed $alias
32
     * @return mixed
33
     * @throws InvalidArgumentException    When cant retrieve filter from service manager.
34
     */
35
    public function get($alias)
36
    {
37
        if (parent::has($alias)) {
38
            return parent::get($alias);
39
        }
40
41
        $service = $this->container;
42
        if (!$service->has($alias)) {
43
            throw new InvalidArgumentException(
44
                sprintf(
45
                    'There is no "%s" filter in Laminas service manager.',
46
                    $alias
47
                )
48
            );
49
        }
50
51
        $filter = $service->get($alias);
52
        if (!($filter instanceof FilterInterface)) {
53
            $givenType = is_object($filter) ? get_class($filter) : gettype($filter);
54
            $message   = 'Retrieved filter "%s" is not instanceof "Assetic\Filter\FilterInterface", but type was given %s';
55
            $message   = sprintf($message, $alias, $givenType);
56
            throw new InvalidArgumentException($message);
57
        }
58
59
        $this->set($alias, $filter);
60
61
        return $filter;
62
    }
63
64
}
65