Completed
Push — drivers ( b4fa87...a5e726 )
by Joe
04:54 queued 21s
created

EventHistoryProvider::hasNotFired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Support\Events;
4
5
use Closure;
6
use Countable;
7
use PhpWinTools\WmiScripting\Configuration\Config;
8
9
class EventHistoryProvider implements Countable
10
{
11
    /** @var Config */
12
    protected $config;
13
14
    /** @var array|FiredEvent[] */
15
    protected $fired_events = [];
16
17
    /** @var array */
18
    protected $event_cache = [
19
        'actual'    => [],
20
        'ancestors' => [],
21
        'listeners' => [],
22
    ];
23
24
    public function __construct(Config $config = null)
25
    {
26
        $this->config = $config ?? Config::instance();
27
    }
28
29
30
    /**
31
     * @param Event            $event
32
     * @param array|Listener[] $listeners
33
     *
34
     * @return self
35
     */
36
    public function add(Event $event, array $listeners = []): self
37
    {
38
        if ($this->shouldNotTrack()) {
39
            return $this;
40
        }
41
42
        $this->fired_events[] = new FiredEvent($event, $listeners);
43
44
        return $this->cacheEvent($event, $listeners);
45
    }
46
47
    /**
48
     * @return array|FiredEvent[]
49
     */
50
    public function all()
51
    {
52
        return $this->fired_events;
53
    }
54
55
    /**
56
     * @param string              $event
57
     * @param array|Closure|mixed $default
58
     *
59
     * @return array|FiredEvent[]|mixed
60
     */
61
    public function get(string $event, $default = [])
62
    {
63
        return $this->findEvents($this->getFiredEventKeys($event), $default);
64
    }
65
66
    /**
67
     * @param string              $listener
68
     * @param array|Closure|mixed $default
69
     *
70
     * @return array|FiredEvent[]|mixed
71
     */
72
    public function getFromListener(string $listener, $default = [])
73
    {
74
        return $this->findEvents($this->event_cache['listeners'][$listener] ?? [], $default);
75
    }
76
77
    /**
78
     * @param string $event
79
     *
80
     * @return array|mixed
81
     */
82
    public function getFiredEventKeys(string $event)
83
    {
84
        $keys = [];
85
86
        if ($this->wasFiredByName($event)) {
87
            $keys = $this->event_cache['actual'][$event];
88
        }
89
90
        if ($this->wasFiredByDescendant($event)) {
91
            $keys = array_merge($keys, $this->event_cache['ancestors'][$event]);
92
        }
93
94
        return $keys;
95
    }
96
97
    /**
98
     * @return int
99
     */
100
    public function count(): int
101
    {
102
        return count($this->fired_events);
103
    }
104
105
    /**
106
     * @param string|null $event
107
     *
108
     * @return int
109
     */
110
    public function eventCount(string $event = null): int
111
    {
112
        return is_null($event) ? $this->count() : count($this->get($event));
113
    }
114
115
    /**
116
     * @return int
117
     */
118
    public function lastIndex(): int
119
    {
120
        return $this->count() - 1;
121
    }
122
123
    /**
124
     * @param string $event
125
     *
126
     * @return bool
127
     */
128
    public function hasFired(string $event): bool
129
    {
130
        return $this->wasFiredByName($event) || $this->wasFiredByDescendant($event);
131
    }
132
133
    /**
134
     * @param string $event
135
     *
136
     * @return bool
137
     */
138
    public function hasNotFired(string $event): bool
139
    {
140
        return $this->hasFired($event) === false;
141
    }
142
143
    /**
144
     * @param string $event
145
     *
146
     * @return bool
147
     */
148
    public function wasFiredByName(string $event): bool
149
    {
150
        return array_key_exists($event, $this->event_cache['actual']);
151
    }
152
153
    /**
154
     * @param string $event
155
     *
156
     * @return bool
157
     */
158
    public function wasFiredByDescendant(string $event): bool
159
    {
160
        return array_key_exists($event, $this->event_cache['ancestors']);
161
    }
162
163
    /**
164
     * @param array               $event_keys
165
     * @param array|Closure|mixed $default
166
     *
167
     * @return array|FiredEvent[]|mixed
168
     */
169
    protected function findEvents(array $event_keys = [], $default = [])
170
    {
171
        $events = array_values(array_map(function ($event_key) {
172
            return $this->fired_events[$event_key];
173
        }, $event_keys));
174
175
        if (empty($events)) {
176
            return is_callable($default) ? $default() : $default;
177
        }
178
179
        return $events;
180
    }
181
182
    /**
183
     * @param Event            $event
184
     * @param array|Listener[] $listeners
185
     *
186
     * @return self
187
     */
188
    protected function cacheEvent(Event $event, array $listeners = []): self
189
    {
190
        $this->event_cache['actual'][get_class($event)][] = $this->lastIndex();
191
192
        return $this->cacheSet($event, 'ancestors', class_parents($event))->cacheSet($event, 'listeners', $listeners);
193
    }
194
195
    protected function cacheSet(Event $event, string $set_name, array $set)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

195
    protected function cacheSet(/** @scrutinizer ignore-unused */ Event $event, string $set_name, array $set)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
196
    {
197
        array_map(function ($item) use ($set_name) {
198
            $item = is_object($item) ? get_class($item) : $item;
199
            $this->event_cache[$set_name][$item][] = $this->lastIndex();
200
        }, $set);
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return bool
207
     */
208
    protected function shouldTrack(): bool
209
    {
210
        return $this->config->shouldTrackEvents();
211
    }
212
213
    /**
214
     * @return bool
215
     */
216
    protected function shouldNotTrack(): bool
217
    {
218
        return $this->shouldTrack() === false;
219
    }
220
}
221