Completed
Push — master ( f4415a...651f7b )
by Anton
12s
created

Event::getParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 13
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\EventManager;
12
13
/**
14
 * Representation of an event
15
 *
16
 * @package  Bluz\EventManager
17
 */
18
class Event
19
{
20
    /**
21
     * @var string event name
22
     */
23
    protected $name;
24
25
    /**
26
     * @var string|object the event target
27
     */
28
    protected $target;
29
30
    /**
31
     * @var array|object the event parameters
32
     */
33
    protected $params = [];
34
35
    /**
36
     * Constructor
37
     *
38
     * Accept a target and its parameters.
39
     *
40
     * @param  string        $name Event name
41
     * @param  string|object $target
42
     * @param  array|object  $params
43
     *
44
     * @throws EventException
45
     */
46 11
    public function __construct($name, $target = null, $params = null)
47
    {
48 11
        $this->setName($name);
49
50 11
        if (null !== $target) {
51 4
            $this->setTarget($target);
52
        }
53
54 11
        if (null !== $params) {
55 5
            $this->setParams($params);
56
        }
57 9
    }
58
59
    /**
60
     * Get event name
61
     *
62
     * @return string
63
     */
64 8
    public function getName() : string
65
    {
66 8
        return $this->name;
67
    }
68
69
    /**
70
     * Get the event target
71
     *
72
     * This may be either an object, or the name of a static method.
73
     *
74
     * @return string|object
75
     */
76 8
    public function getTarget()
77
    {
78 8
        return $this->target;
79
    }
80
81
    /**
82
     * Overwrites parameters
83
     *
84
     * @param  array|object $params
85
     *
86
     * @return void
87
     * @throws EventException
88
     */
89 5
    public function setParams($params) : void
90
    {
91 5
        if (!\is_array($params) && !\is_object($params)) {
92 2
            throw new EventException(
93 2
                'Event parameters must be an array or object; received `' . \gettype($params) . '`'
94
            );
95
        }
96
97 3
        $this->params = $params;
98 3
    }
99
100
    /**
101
     * Get all parameters
102
     *
103
     * @return array|object
104
     */
105 2
    public function getParams()
106
    {
107 2
        return $this->params;
108
    }
109
110
    /**
111
     * Get an individual parameter
112
     *
113
     * If the parameter does not exist, the $default value will be returned.
114
     *
115
     * @param  string|int $name
116
     * @param  mixed      $default
117
     *
118
     * @return mixed
119
     */
120 3
    public function getParam($name, $default = null)
121
    {
122 3
        if (\is_array($this->params)) {
123
            // Check in params that are arrays or implement array access
124 2
            return $this->params[$name] ?? $default;
125 1
        } elseif (\is_object($this->params)) {
126
            // Check in normal objects
127 1
            return $this->params->{$name} ?? $default;
128
        } else {
129
            // Wrong type, return default value
130
            return $default;
131
        }
132
    }
133
134
    /**
135
     * Set the event name
136
     *
137
     * @param  string $name
138
     *
139
     * @return void
140
     */
141 11
    public function setName($name) : void
142
    {
143 11
        $this->name = (string)$name;
144 11
    }
145
146
    /**
147
     * Set the event target/context
148
     *
149
     * @param  null|string|object $target
150
     *
151
     * @return void
152
     */
153 4
    public function setTarget($target) : void
154
    {
155 4
        $this->target = $target;
156 4
    }
157
158
    /**
159
     * Set an individual parameter to a value
160
     *
161
     * @param  string|int $name
162
     * @param  mixed      $value
163
     *
164
     * @return void
165
     */
166 2
    public function setParam($name, $value) : void
167
    {
168 2
        if (\is_array($this->params)) {
169
            // Arrays or objects implementing array access
170 1
            $this->params[$name] = $value;
171
        } else {
172
            // Objects
173 1
            $this->params->{$name} = $value;
174
        }
175 2
    }
176
}
177