Passed
Push — develop ( 528c82...91706d )
by Peter
03:12 queued 10s
created

EventEmitterTrait::getEventDispatcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/event-emitter
6
 * @copyright   Copyright (c) 2019 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace Webino;
12
13
/**
14
 * Trait EventEmitterTrait
15
 * @package event-emitter
16
 */
17
trait EventEmitterTrait
18
{
19
    /**
20
     * @var EventEmitterInterface
21
     */
22
    protected $eventDispatcher;
23
24
    /**
25
     * Returns event dispatcher
26
     *
27
     * @return EventEmitterInterface
28
     */
29
    public function getEventDispatcher(): EventEmitterInterface
30
    {
31
        if (!$this->eventDispatcher) {
32
            $this->setEventDispatcher(new EventDispatcher);
33
        }
34
35
        return $this->eventDispatcher;
36
    }
37
38
    /**
39
     * Inject event dispatcher.
40
     *
41
     * @param EventEmitterInterface $eventEmitter
42
     */
43
    public function setEventDispatcher(EventEmitterInterface $eventEmitter): void
44
    {
45
        $this->eventDispatcher = $eventEmitter;
46
    }
47
48
    /**
49
     * Invoke handlers
50
     *
51
     * @param string|EventInterface $event Event name or object
52
     * @param callable|null $until Invoke handlers until callback return value evaluate to true
53
     * @return EventInterface Event object
54
     * @throws InvalidEventException Invalid event
55
     */
56
    public function emit($event, callable $until = null): EventInterface
57
    {
58
        return $this->getEventDispatcher()->emit($event, $until);
59
    }
60
61
    /**
62
     * Set event handler
63
     *
64
     * @param string|EventInterface|EventHandlerInterface $event Event name, object or event handler
65
     * @param callable|null $callback Event handler
66
     * @param int $priority Handler invocation priority
67
     * @return void
68
     */
69
    public function on($event, $callback = null, int $priority = 0)
70
    {
71
        $this->getEventDispatcher()->on($event, $callback, $priority);
72
    }
73
74
    /**
75
     * Remove event handler
76
     *
77
     * @param callable|EventHandlerInterface|null $callback Event handler
78
     * @param string|EventInterface|null $event Event name or object
79
     * @return void
80
     */
81
    public function off($callback = null, $event = null): void
82
    {
83
        $this->getEventDispatcher()->off($callback, $event);
84
    }
85
}
86