Passed
Pull Request — master (#238)
by Dmitriy
02:27
created

EventConfigurator::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web\Config;
4
5
use Psr\Container\ContainerInterface;
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
use Yiisoft\EventDispatcher\Provider\ConcreteProvider;
0 ignored issues
show
Bug introduced by
The type Yiisoft\EventDispatcher\Provider\ConcreteProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Yiisoft\EventDispatcher\Provider\Provider;
0 ignored issues
show
Bug introduced by
The type Yiisoft\EventDispatcher\Provider\Provider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class EventConfigurator
11
{
12
    private ListenerProviderInterface $listenerProvider;
13
14
    private ContainerInterface $container;
15
16
    public function __construct(ListenerProviderInterface $listenerProvider, ContainerInterface $container)
17
    {
18
        if (!($listenerProvider instanceof Provider || $listenerProvider instanceof ConcreteProvider)) {
19
            throw new \InvalidArgumentException(
20
                'Listener provider must be instance of Yiisoft\EventDispatcher\Provider or
21
                Yiisoft\EventDispatcher\ConcreteProvider.'
22
            );
23
        }
24
        $this->listenerProvider = $listenerProvider;
25
        $this->container = $container;
26
    }
27
28
    public function registerListeners(array $listeners): void
29
    {
30
        foreach ($listeners as $event => $listener) {
31
            if (is_string($event)) {
32
                foreach ($listener as $callable) {
33
                    if (!is_callable($callable)) {
34
                        throw new \RuntimeException('Listener must be a callable.');
35
                    }
36
                    if (is_array($callable) && !is_object($callable[0])) {
37
                        $callable = [$this->container->get($callable[0]), $callable[1]];
38
                    }
39
                    $this->listenerProvider->attach($callable, $event);
0 ignored issues
show
Bug introduced by
The method attach() does not exist on Psr\EventDispatcher\ListenerProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
                    $this->listenerProvider->/** @scrutinizer ignore-call */ 
40
                                             attach($callable, $event);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
                }
41
            } else {
42
                if (!is_callable($listener)) {
43
                    throw new \RuntimeException('Listener must be a callable.');
44
                }
45
                $this->listenerProvider->attach($listener);
46
            }
47
        }
48
    }
49
}
50