Completed
Pull Request — master (#67)
by Eric
03:10
created

BroadcasterTrait::on()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jarvis\Skill\EventBroadcaster;
6
7
/**
8
 * @author Eric Chau <[email protected]>
9
 */
10
trait BroadcasterTrait
11
{
12
    private $receivers = [];
13
    private $permanentEvents = [];
14
    private $computedReceivers = [];
15
    private $masterEmitter = false;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function on(string $name, $receiver, int $priority = BroadcasterInterface::RECEIVER_NORMAL_PRIORITY): void
21
    {
22
        if (!isset($this->receivers[$name])) {
23
            $this->receivers[$name] = [
24
                BroadcasterInterface::RECEIVER_LOW_PRIORITY    => [],
25
                BroadcasterInterface::RECEIVER_NORMAL_PRIORITY => [],
26
                BroadcasterInterface::RECEIVER_HIGH_PRIORITY   => [],
27
            ];
28
        }
29
30
        $this->receivers[$name][$priority][] = $receiver;
31
        $this->computedReceivers[$name] = null;
32
        if (isset($this->permanentEvents[$name])) {
33
            $this['callbackResolver']->resolveAndCall($receiver, [
34
                'event' => $this->permanentEvents[$name],
35
            ]);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function broadcast(string $name, EventInterface $event = null): void
43
    {
44
        if (!$this->masterEmitter && in_array($name, BroadcasterInterface::RESERVED_EVENT_NAMES)) {
45
            throw new \LogicException(sprintf(
46
                'You\'re trying to broadcast "$name" but "%s" are reserved event names.',
47
                implode('|', BroadcasterInterface::RESERVED_EVENT_NAMES)
48
            ));
49
        }
50
51
        if (isset($this->permanentEvents[$name])) {
52
            throw new \LogicException('Permanent event cannot be broadcasted multiple times.');
53
        }
54
55
        $event = $event ?? new SimpleEvent();
56
        if ($event instanceof PermanentEventInterface && $event->isPermanent()) {
57
            $this->permanentEvents[$name] = $event;
58
        }
59
60
        if (isset($this->receivers[$name])) {
61
            foreach ($this->buildEventReceivers($name) as $receiver) {
62
                $this['callbackResolver']->resolveAndCall($receiver, [
63
                    'event' => $event,
64
                ]);
65
                if ($event->isPropagationStopped()) {
66
                    break;
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * Enables master emitter mode.
74
     *
75
     * @return self
76
     */
77
    private function masterBroadcast(string $name, EventInterface $event = null): void
78
    {
79
        $this->masterEmitter = true;
80
        $this->broadcast($name, $event);
81
        $this->masterEmitter = false;
82
    }
83
84
    /**
85
     * Builds and returns well ordered receivers collection that match with provided event name.
86
     *
87
     * @param  string $name The event name we want to get its receivers
88
     * @return array
89
     */
90
    private function buildEventReceivers(string $name): array
91
    {
92
        return $this->computedReceivers[$name] = $this->computedReceivers[$name] ?? array_merge(
93
            $this->receivers[$name][BroadcasterInterface::RECEIVER_HIGH_PRIORITY],
94
            $this->receivers[$name][BroadcasterInterface::RECEIVER_NORMAL_PRIORITY],
95
            $this->receivers[$name][BroadcasterInterface::RECEIVER_LOW_PRIORITY]
96
        );
97
    }
98
}
99