PriorityListenerProvider::attach()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\EventDispatcher\ListenerProviders;
4
5
use Psr\EventDispatcher\ListenerProviderInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ByTIC\EventDispatcher\Li...stenerProviderInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * Class PriorityListenerProvider
9
 * @package ByTIC\EventDispatcher\ListenerProviders
10
 */
11
class PriorityListenerProvider implements ListenerProviderInterface
12
{
13
    use Traits\ProviderUtilitiesTrait;
14
    use Traits\MakeListenerTrait;
15
16
    protected $listenersPerEvent = [];
17
18
    protected function getListenersForEventName(string $eventName): iterable
19
    {
20
        if (!array_key_exists($eventName, $this->listenersPerEvent)) {
21
            return [];
22
        }
23
24
        return $this->listenersPerEvent[$eventName]->getListeners();
25
    }
26
27
    /**
28
     * @param callable|string $listener
29
     * @param int $priority
30
     * @param string|null $event
31
     * @return void
32
     */
33
    public function attach(
34
        $listener,
35
        int $priority = 0,
36
        string $event = null
37
    ): void {
38
        $listener = self::makeListener($listener);
0 ignored issues
show
Bug Best Practice introduced by
The method ByTIC\EventDispatcher\Li...rovider::makeListener() is not static, but was called statically. ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $listener = self::makeListener($listener);
Loading history...
39
        $event = $event ?? $this->getParameterType($listener);
40
        $this->subscribeTo($event, $listener, $priority);
41
    }
42
43
44
    public function subscribeTo(string $event, callable $listener, int $priority = 0): void
45
    {
46
        $group = array_key_exists($event, $this->listenersPerEvent)
47
            ? $this->listenersPerEvent[$event]
48
            : $this->listenersPerEvent[$event] = new PrioritizedListenersForEvent();
49
50
        $group->addListener($listener, $priority);
51
    }
52
53
    public function subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void
54
    {
55
        $this->subscribeTo($event, new OneTimeListener($listener), $priority);
56
    }
57
58
    public function getListenersForEvent(object $event): iterable
59
    {
60
        /**
61
         * @var string $key
62
         * @var PrioritizedListenersForEvent $group
63
         */
64
        foreach ($this->listenersPerEvent as $key => $group) {
65
            if ($event instanceof $key) {
66
                yield from $group->getListeners();
67
            }
68
        }
69
70
        if (method_exists($event, 'eventName')) {
71
            yield from $this->getListenersForEventName($event->eventName());
72
        }
73
    }
74
75
//    public function subscribeListenersFrom(ListenerSubscriber $subscriber): void
76
//    {
77
//        $subscriber->subscribeListeners($this);
78
//    }
79
}
80