Event::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Event
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Event;
16
17
use Phossa2\Event\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Event\Interfaces\EventInterface;
20
use Phossa2\Event\Exception\InvalidArgumentException;
21
22
/**
23
 * Event
24
 *
25
 * Basic implementation of EventInterface
26
 *
27
 * ```php
28
 * // create event
29
 * $evt = new Event(
30
 *     'login.attempt',         // event name
31
 *     $this,                   // event target
32
 *     ['username' => 'phossa'] // event parameters
33
 * );
34
 *
35
 * // stop event
36
 * $evt->stopPropagation();
37
 * ```
38
 *
39
 * @package Phossa2\Event
40
 * @author  Hong Zhang <[email protected]>
41
 * @see     ObjectAbstract
42
 * @see     EventInterface
43
 * @version 2.0.0
44
 * @since   2.0.0 added
45
 * @since   2.1.0 using psr EventInterface now
46
 * @since   2.1.1 removed ArrayAccess
47
 */
48
class Event extends ObjectAbstract implements EventInterface
49
{
50
    /**
51
     * event name
52
     *
53
     * @var    string
54
     * @access protected
55
     */
56
    protected $name;
57
58
    /**
59
     * event target/context
60
     *
61
     * an object OR static class name (string)
62
     *
63
     * @var    object|string|null
64
     * @access protected
65
     */
66
    protected $target;
67
68
    /**
69
     * event parameters
70
     *
71
     * @var    array
72
     * @access protected
73
     */
74
    protected $parameters;
75
76
    /**
77
     * stop propagation
78
     *
79
     * @var    bool
80
     * @access protected
81
     */
82
    protected $stopped = false;
83
84
    /**
85
     * Constructor
86
     *
87
     * @param  string $eventName event name
88
     * @param  string|object|null $target event context, object or classname
89
     * @param  array $parameters (optional) event parameters
90
     * @throws InvalidArgumentException if event name is invalid
91
     * @access public
92
     * @api
93
     */
94
    public function __construct(
95
        /*# string */ $eventName,
96
        $target = null,
97
        array $parameters = []
98
    ) {
99
        $this->setName($eventName);
100
        $this->setTarget($target);
101
        $this->setParams($parameters);
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function getName()
108
    {
109
        return $this->name;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function getTarget()
116
    {
117
        return $this->target;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function getParams()
124
    {
125
        return $this->parameters;
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function getParam($name)
132
    {
133
        if (isset($this->parameters[(string) $name])) {
134
            return $this->parameters[(string) $name];
135
        }
136
        return null;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function setName($name)
143
    {
144
        if (!is_string($name) || empty($name)) {
145
            throw new InvalidArgumentException(
146
                Message::get(Message::EVT_NAME_INVALID, $name),
147
                Message::EVT_NAME_INVALID
148
            );
149
        }
150
        $this->name = $name;
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156
    public function setTarget($target)
157
    {
158
        $this->target = $target;
159
    }
160
161
    /**
162
     * {@inheritDoc}
163
     */
164
    public function setParams(array $params)
165
    {
166
        $this->parameters = $params;
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172
    public function stopPropagation($flag)
173
    {
174
        $this->stopped = (bool) $flag;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180
    public function isPropagationStopped()
181
    {
182
        return $this->stopped;
183
    }
184
}
185