Completed
Push — master ( 7eb3b6...d299b5 )
by Anton
11s
created

Event::getParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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