Event::__set()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Noair;
4
5
/**
6
 * Event Class.
7
 *
8
 * Objects of this class will be passed, whenever an event is fired, to all
9
 * handlers of said event along with their results. This class also allows
10
 * event handlers to easily share information with other event handlers.
11
 *
12
 * Extend this class if you want to impose some sort of structure on the data
13
 * contained in your specific event type. You could validate the $data array or
14
 * add custom properties.
15
 *
16
 * @author  Garrett Whitehorn
17
 * @author  David Tkachuk
18
 *
19
 * @version 1.0
20
 *
21
 * @property    string  $name   The name of the event
22
 * @property    mixed   $data   Contains the event's data
23
 * @property    mixed   $caller Who fired this event
24
 * @property    bool    $cancelled  Indicates if the event is cancelled
25
 * @property    Mediator|null   $mediator   An instance of the main Mediator class
26
 * @property    array   $previousResults    Contains the results of previous event handlers
27
 * @property    mixed   $previousResult Get = last result, set = adds a new result
28
 */
29
class Event
30
{
31
    /**
32
     * @api
33
     *
34
     * @var string The name of the event
35
     *
36
     * @since   1.0
37
     */
38
    private $name;
39
40
    /**
41
     * @api
42
     *
43
     * @var mixed Contains the event's data
44
     *
45
     * @since   1.0
46
     */
47
    private $data;
48
49
    /**
50
     * @api
51
     *
52
     * @var mixed Who fired this event
53
     *
54
     * @since   1.0
55
     */
56
    private $caller;
57
58
    /**
59
     * @api
60
     *
61
     * @var bool Indicates if the event is cancelled
62
     *
63
     * @since   1.0
64
     */
65
    private $cancelled = false;
66
67
    /**
68
     * @api
69
     *
70
     * @var Mediator|null An instance of the main Mediator class
71
     *
72
     * @since   1.0
73
     */
74
    private $mediator = null;
75
76
    /**
77
     * @api
78
     *
79
     * @var array Contains the results of previous event handlers
80
     *
81
     * @since   1.0
82
     */
83
    private $previousResults = [];
84
85
    /**
86
     * Constructor method of Event.
87
     *
88
     * All of these properties' usage details are left up to the event handler,
89
     * so see your event handler to know what to pass here.
90
     *
91
     * @api
92
     *
93
     * @param string $name   The name of the event
94
     * @param mixed  $data   Data to be used by the event's handler (optional)
95
     * @param mixed  $caller The calling object or class name (optional)
96
     *
97
     * @since   1.0
98
     *
99
     * @version 1.0
100
     */
101
    public function __construct($name, $data = null, $caller = null)
102
    {
103
        $this->name = $name;
104
        $this->data = $data;
105
        $this->caller = $caller;
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function __get($name)
112
    {
113
        if ($name == 'previousResult') {
114
            return end($this->previousResults);
115
        }
116
117
        return $this->$name;
118
    }
119
120
    public function __set($name, $val)
121
    {
122
        switch ($name) {
123
            case 'previousResult':
124
                $this->previousResults[] = $val;
125
                break;
126
127
            case 'cancelled':
128
                $this->cancelled = (bool) $val;
129
                break;
130
131
            case 'mediator':
132
                if ($val instanceof Observable || $val === null) {
133
                    $this->mediator = $val;
134
                }
135
                break;
136
137
            default:
138
                $this->$name = $val;
139
        }
140
    }
141
}
142