|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MallardDuck\DynamicEcho\Collections; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use MallardDuck\DynamicEcho\Loader\LoadedEventDTO; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @example This is an example shape of how this collection should store data. |
|
10
|
|
|
* |
|
11
|
|
|
* items: [ |
|
12
|
|
|
* "App.Models.User.{userId}" => ChannelEventCollection { |
|
13
|
|
|
* channel: "App.Models.User.{userId}", |
|
14
|
|
|
* channelAuthCallback: someCallbackMethod, |
|
15
|
|
|
* items: [ |
|
16
|
|
|
* "ToastEvent" => MallardDuck\DynamicEcho\Loader\LoadedEventDTO |
|
17
|
|
|
* ] |
|
18
|
|
|
* }, |
|
19
|
|
|
* "App.Models.Game.{gameId}.User.{userId}" => ChannelEventCollection { |
|
20
|
|
|
* channel: "App.Models.Game.{gameId}.User.{userId}", |
|
21
|
|
|
* channelAuthCallback: someCallbackMethod, |
|
22
|
|
|
* items: [ |
|
23
|
|
|
* "SomeEvent" => MallardDuck\DynamicEcho\Loader\LoadedEventDTO, |
|
24
|
|
|
* "SomeEvent2" => MallardDuck\DynamicEcho\Loader\LoadedEventDTO, |
|
25
|
|
|
* ] |
|
26
|
|
|
* } |
|
27
|
|
|
* ] |
|
28
|
|
|
*/ |
|
29
|
|
|
class ChannelAwareEventCollection extends Collection |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* The items contained in the collection. |
|
33
|
|
|
* |
|
34
|
|
|
* @var ChannelEventCollection[] $items |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $items = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Push one or more items onto the end of the collection. |
|
40
|
|
|
* |
|
41
|
|
|
* @param LoadedEventDTO|LoadedEventDTO[] $values [optional] |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function push(...$values) |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($values as $key => $value) { |
|
47
|
|
|
$identifier = $value->getParameter('channelAuthName'); |
|
48
|
|
|
|
|
49
|
|
|
if (!$this->has($identifier)) { |
|
50
|
|
|
$channelEventCollection = ChannelEventCollection::new( |
|
51
|
|
|
$identifier, |
|
52
|
|
|
$value->getChannelParameters() |
|
53
|
|
|
); |
|
54
|
|
|
} else { |
|
55
|
|
|
$channelEventCollection = $this->get($identifier); |
|
56
|
|
|
} |
|
57
|
|
|
$channelEventCollection = $channelEventCollection->push($value); |
|
58
|
|
|
$this->items[$identifier] = $channelEventCollection; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function pop() |
|
65
|
|
|
{ |
|
66
|
|
|
throw new \BadMethodCallException( |
|
67
|
|
|
"This method should not be called use an alternatives; either popChannel or popChannelEvent." |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get and remove the last item from the collection. |
|
73
|
|
|
* |
|
74
|
|
|
* @return ChannelEventCollection|null |
|
75
|
|
|
*/ |
|
76
|
|
|
public function popChannel(): ?ChannelEventCollection |
|
77
|
|
|
{ |
|
78
|
|
|
return array_pop($this->items); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get and remove the last item from the collection. |
|
83
|
|
|
* |
|
84
|
|
|
* Just like normal array_pop, if the array is empty it will return null. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $channelName The name of the Channel that we're popping an event from. |
|
87
|
|
|
* |
|
88
|
|
|
* @return LoadedEventDTO|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function popChannelEvent(string $channelName): ?LoadedEventDTO |
|
91
|
|
|
{ |
|
92
|
|
|
if (array_key_exists($channelName, $this->items)) { |
|
93
|
|
|
return $this->items[$channelName]->pop(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|