Completed
Push — drivers ( fe89be...446647 )
by Joe
01:59
created

EventHistoryProvider::lastIndex()   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 0
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
    protected $eventContainer;
25
26
    public function __construct(Config $config = null)
27
    {
28
        $this->config = $config ?? Config::instance();
29
30
        $this->eventContainer = new EventCacheContainer();
31
    }
32
33
    public function container()
34
    {
35
        return $this->eventContainer;
36
    }
37
38
    /**
39
     * @param Event            $event
40
     * @param array|Listener[] $listeners
41
     *
42
     * @return self
43
     */
44
    public function add(Event $event, array $listeners = []): self
45
    {
46
        if ($this->shouldNotTrack()) {
47
            return $this;
48
        }
49
50
        $this->fired_events[] = new FiredEvent($event, $listeners);
51
52
        return $this->cacheEvent($event, $listeners);
53
    }
54
55
    /**
56
     * @return array|FiredEvent[]
57
     */
58
    public function all()
59
    {
60
        return $this->fired_events;
61
    }
62
63
    /**
64
     * @param string              $event
65
     * @param array|Closure|mixed $default
66
     *
67
     * @return array|FiredEvent[]|mixed
68
     */
69
    public function get(string $event, $default = [])
70
    {
71
        return $this->findEvents($this->getFiredEventKeys($event), $default);
72
    }
73
74
    /**
75
     * @param string              $listener
76
     * @param array|Closure|mixed $default
77
     *
78
     * @return array|FiredEvent[]|mixed
79
     */
80
    public function getFromListener(string $listener, $default = [])
81
    {
82
        return $this->findEvents($this->event_cache['listeners'][$listener] ?? [], $default);
83
    }
84
85
    /**
86
     * @param string $event
87
     *
88
     * @return array|mixed
89
     */
90
    public function getFiredEventKeys(string $event)
91
    {
92
        $keys = [];
93
94
        if ($this->wasFiredByName($event)) {
95
            $keys = $this->event_cache['actual'][$event];
96
        }
97
98
        if ($this->wasFiredByDescendant($event)) {
99
            $keys = array_merge($keys, $this->event_cache['ancestors'][$event]);
100
        }
101
102
        return $keys;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function count(): int
109
    {
110
        return count($this->fired_events);
111
    }
112
113
    /**
114
     * @param string|null $event
115
     *
116
     * @return int
117
     */
118
    public function eventCount(string $event = null): int
119
    {
120
        return is_null($event) ? $this->count() : count($this->get($event));
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    public function lastIndex(): int
127
    {
128
        return $this->count() - 1;
129
    }
130
131
    /**
132
     * @param string $event
133
     *
134
     * @return bool
135
     */
136
    public function hasFired(string $event): bool
137
    {
138
        return $this->wasFiredByName($event) || $this->wasFiredByDescendant($event);
139
    }
140
141
    /**
142
     * @param string $event
143
     *
144
     * @return bool
145
     */
146
    public function hasNotFired(string $event): bool
147
    {
148
        return $this->hasFired($event) === false;
149
    }
150
151
    /**
152
     * @param string $event
153
     *
154
     * @return bool
155
     */
156
    public function wasFiredByName(string $event): bool
157
    {
158
        return array_key_exists($event, $this->event_cache['actual']);
159
    }
160
161
    /**
162
     * @param string $event
163
     *
164
     * @return bool
165
     */
166
    public function wasFiredByDescendant(string $event): bool
167
    {
168
        return array_key_exists($event, $this->event_cache['ancestors']);
169
    }
170
171
    /**
172
     * @param array               $event_keys
173
     * @param array|Closure|mixed $default
174
     *
175
     * @return array|FiredEvent[]|mixed
176
     */
177
    protected function findEvents(array $event_keys = [], $default = [])
178
    {
179
        $events = array_values(array_map(function ($event_key) {
180
            return $this->fired_events[$event_key];
181
        }, $event_keys));
182
183
        if (empty($events)) {
184
            return is_callable($default) ? $default() : $default;
185
        }
186
187
        return $events;
188
    }
189
190
    /**
191
     * @param Event            $event
192
     * @param array|Listener[] $listeners
193
     *
194
     * @return self
195
     */
196
    protected function cacheEvent(Event $event, array $listeners = []): self
197
    {
198
        $this->event_cache['actual'][get_class($event)][] = $this->lastIndex();
199
200
        $this->cacheEventSet($event, 'ancestors', class_parents($event), function ($item) {
201
            return $item;
202
        });
203
204
        $this->cacheEventSet($event, 'listeners', $listeners, function (Listener $listener) {
205
            return get_class($listener);
206
        });
207
208
        return $this;
209
    }
210
211
    protected function cacheEventSet(Event $event, string $set_name, array $set, Closure $getItemKey)
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

211
    protected function cacheEventSet(/** @scrutinizer ignore-unused */ Event $event, string $set_name, array $set, Closure $getItemKey)

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...
212
    {
213
        array_map(function ($item) use ($set_name, $getItemKey) {
214
            $this->event_cache[$set_name][$getItemKey($item)][] = $this->lastIndex();
215
        }, $set);
216
    }
217
218
    /**
219
     * @return bool
220
     */
221
    protected function shouldTrack(): bool
222
    {
223
        return $this->config->shouldTrackEvents();
224
    }
225
226
    /**
227
     * @return bool
228
     */
229
    protected function shouldNotTrack(): bool
230
    {
231
        return $this->shouldTrack() === false;
232
    }
233
}
234