EventHandlerTrait::attach()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Da\Mailer\Event;
4
5
trait EventHandlerTrait
6
{
7
    /**
8
     * @var array stack of events attached to the manager
9
     */
10
    protected $events = [];
11
/**
12
     * Adds an Event instance to the stack based on the name.
13
     *
14
     * @param string $name the identifier of the stack
15
     * @param Event $event the event instance to add
16
     */
17 4
    public function attach($name, Event $event)
18
    {
19 4
        if (!isset($this->events[$name])) {
20 4
            $this->events[$name] = [];
21 4
        }
22
23 4
        $this->events[$name][] = $event;
24 4
    }
25
26
    /**
27
     * Removes the handlers of stack by its name.
28
     *
29
     * @param string $name
30
     */
31 1
    public function detach($name)
32
    {
33 1
        if (array_key_exists($name, $this->events)) {
34 1
            unset($this->events[$name]);
35 1
        }
36 1
    }
37
38
    /**
39
     * Fires the handlers of a stack by its name.
40
     *
41
     * @param string $name the name of the stack to fire
42
     * @param array $data
43
     */
44 4
    public function trigger($name, array $data = [])
45
    {
46 4
        if (isset($this->events[$name])) {
47 4
            foreach ($this->events[$name] as $event) {
48 4
                $event->owner = $this;
49 4
                call_user_func_array($event, $data);
50 4
            }
51 4
        }
52 4
    }
53
}
54