Completed
Pull Request — master (#67)
by Eric
02:05
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 (isset($this->permanentEvents[$name])) {
45
            throw new \LogicException('Permanent event cannot be broadcasted multiple times.');
46
        }
47
48
        $event = $event ?? new SimpleEvent();
49
        if ($event instanceof PermanentEventInterface && $event->isPermanent()) {
50
            $this->permanentEvents[$name] = $event;
51
        }
52
53
        if (isset($this->receivers[$name])) {
54
            foreach ($this->buildEventReceivers($name) as $receiver) {
55
                $this['callbackResolver']->resolveAndCall($receiver, [
56
                    'event' => $event,
57
                ]);
58
                if ($event->isPropagationStopped()) {
59
                    break;
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * Builds and returns well ordered receivers collection that match with provided event name.
67
     *
68
     * @param  string $name The event name we want to get its receivers
69
     * @return array
70
     */
71
    private function buildEventReceivers(string $name): array
72
    {
73
        return $this->computedReceivers[$name] = $this->computedReceivers[$name] ?? array_merge(
74
            $this->receivers[$name][BroadcasterInterface::RECEIVER_HIGH_PRIORITY],
75
            $this->receivers[$name][BroadcasterInterface::RECEIVER_NORMAL_PRIORITY],
76
            $this->receivers[$name][BroadcasterInterface::RECEIVER_LOW_PRIORITY]
77
        );
78
    }
79
}
80