Event::getTarget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Event;
4
5
use Psr\EventManager\EventInterface;
6
use InvalidArgumentException;
7
8
class Event implements EventInterface
9
{
10
    private $name;
11
    private $params = [];
12
    private $target;
13
    private $isPropagationStopped = false;
14
15
    /**
16
     * @param string $name
17
     * @param array $params
18
     * @param object | string $target
19
     * @param bool $isPropagationStopped
20
     */
21 12
    public function __construct(
22
        $name,
23
        $params = [],
24
        $target = null,
25
        $isPropagationStopped = false
26
    ) {
27 12
        $this->setName($name);
28 12
        $this->setParams($params);
29 12
        $this->setTarget($target);
30 12
        $this->stopPropagation($isPropagationStopped);
31 12
    }
32
33
    /**
34
     * Get event name
35
     *
36
     * @return string
37
     */
38 5
    public function getName()
39
    {
40 5
        return $this->name;
41
    }
42
43
    /**
44
     * Get target/context from which event was triggered
45
     *
46
     * @return null|string|object
47
     */
48 1
    public function getTarget()
49
    {
50 1
        return $this->target;
51
    }
52
53
    /**
54
     * Get parameters passed to the event
55
     *
56
     * @return array
57
     */
58 1
    public function getParams()
59
    {
60 1
        return $this->params;
61
    }
62
63
    /**
64
     * Get a single parameter by name
65
     *
66
     * @param  string $name
67
     * @return mixed
68
     */
69 1
    public function getParam($name)
70
    {
71 1
        return $this->params[$name];
72
    }
73
74
    /**
75
     * Set the event name
76
     *
77
     * @param  string $name
78
     * @return void
79
     */
80 12
    public function setName($name)
81
    {
82 12
        if (!$this->validateName($name)) {
83 1
            throw new InvalidArgumentException("Invalid event name format (Event name MUST contain \"A-z\", \"0-9\", \"_\", \".\"", 1);
84
        }
85
86 12
        $this->name = $name;
87 12
    }
88
89
    /**
90
     * Set the event target
91
     *
92
     * @param  null|string|object $target
93
     * @return void
94
     */
95 12
    public function setTarget($target)
96
    {
97 12
        $this->target = $target;
98 12
    }
99
100
    /**
101
     * Set event parameters
102
     *
103
     * @param  array $params
104
     * @return void
105
     */
106 12
    public function setParams(array $params)
107
    {
108 12
        $this->params = $params;
109 12
    }
110
111
    /**
112
     * Indicate whether or not to stop propagating this event
113
     *
114
     * @param  bool $flag
115
     * @return void
116
     */
117 12
    public function stopPropagation($flag = true)
118
    {
119 12
        $this->isPropagationStopped = $flag;
120 12
    }
121
122
    /**
123
     * Has this event indicated event propagation should stop?
124
     *
125
     * @return bool
126
     */
127 1
    public function isPropagationStopped()
128
    {
129 1
        return $this->isPropagationStopped;
130
    }
131
132
    /**
133
     * Validate name with PSR specification
134
     *
135
     * @param string $name
136
     * @return bool
137
     */
138 12
    private function validateName($name)
139
    {
140 12
        if (preg_match_all('/[^(A-z0-9_.)]/', $name, $matches, PREG_SET_ORDER, 0)) {
141 1
            return false;
142
        }
143
144 12
        return true;
145
    }
146
}
147