1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MallardDuck\DynamicEcho; |
4
|
|
|
|
5
|
|
|
use MallardDuck\DynamicEcho\Loader\LoadedEventDTO; |
6
|
|
|
use MallardDuck\DynamicEcho\Collections\{ |
7
|
|
|
ChannelEventCollection, |
8
|
|
|
ChannelAwareEventCollection |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
class ChannelManager |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ChannelAwareEventCollection |
15
|
|
|
*/ |
16
|
|
|
private ChannelAwareEventCollection $channelCollection; |
17
|
|
|
|
18
|
28 |
|
public function __construct() |
19
|
|
|
{ |
20
|
28 |
|
$this->channelCollection = new ChannelAwareEventCollection(); |
21
|
28 |
|
} |
22
|
|
|
|
23
|
28 |
|
public function getChannelEventCollection(): ChannelAwareEventCollection |
24
|
|
|
{ |
25
|
28 |
|
return $this->channelCollection; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param LoadedEventDTO $newItem |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function pushEventDto(LoadedEventDTO $newItem): self |
34
|
|
|
{ |
35
|
|
|
$this->channelCollection = $this->channelCollection->push($newItem); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Push multiple EventDTOs into the channel collection. |
42
|
|
|
* |
43
|
|
|
* @param LoadedEventDTO|LoadedEventDTO[] $newItems,... |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function pushEventDtos(...$newItems): self |
48
|
|
|
{ |
49
|
|
|
$this->channelCollection = $this->channelCollection->push(...$newItems); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function registeredChannelNames() |
55
|
|
|
{ |
56
|
|
|
return $this->channelCollection->keys(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function registeredChannelInfo() |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* @var ChannelEventCollection $channelGroup |
63
|
|
|
*/ |
64
|
|
|
return $this->channelCollection->map(static function ($channelGroup, $key) { |
65
|
|
|
$first = true; |
66
|
|
|
$channelInfo = [ |
67
|
|
|
'authName' => $key, |
68
|
|
|
'type' => null, |
69
|
|
|
'authCallback' => null, |
70
|
|
|
]; |
71
|
|
|
/** |
72
|
|
|
* @var LoadedEventDTO $val |
73
|
|
|
*/ |
74
|
|
|
$events = $channelGroup->map(static function ($val) use (&$first, &$channelInfo) { |
75
|
|
|
if ($first) { |
76
|
|
|
$first = false; |
77
|
|
|
$channelInfo['type'] = $val->getParameter('channelType'); |
78
|
|
|
$channelInfo['authCallback'] = $val->getParameter('channelAuthCallback'); |
79
|
|
|
} |
80
|
|
|
return $val->fullEventName; |
81
|
|
|
})->toArray(); |
82
|
|
|
$channelInfo['events'] = $events; |
83
|
|
|
|
84
|
|
|
return $channelInfo; |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|