Completed
Push — drivers ( c650c9...b4fa87 )
by Joe
02:26
created

EventHistoryProvider::cacheEventSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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