1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MallardDuck\DynamicEcho\Collections; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use MallardDuck\DynamicEcho\Channels\AbstractChannelParameters; |
8
|
|
|
use MallardDuck\DynamicEcho\Loader\LoadedEventDTO; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @example This is an example shape of how this collection should store data. |
12
|
|
|
* |
13
|
|
|
* { |
14
|
|
|
* channelIdentifier: "App.Models.User.{userId}", |
15
|
|
|
* channelAuthCallback: someCallbackMethod, |
16
|
|
|
* items: [ |
17
|
|
|
* "ToastEvent" => MallardDuck\DynamicEcho\Loader\LoadedEventDTO |
18
|
|
|
* ] |
19
|
|
|
* } |
20
|
|
|
*/ |
21
|
|
|
class ChannelEventCollection extends Collection |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The items contained in the collection. |
25
|
|
|
* |
26
|
|
|
* @var LoadedEventDTO[] |
27
|
|
|
*/ |
28
|
|
|
protected $items = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The channel route identifier/formula. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
* @example "App.Models.User.{userId}" |
35
|
|
|
*/ |
36
|
|
|
private string $channelAuthName; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* An md5 of the $channelAuthName - used for setting JS context vars. |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
private $channelJsVarKey; |
43
|
|
|
|
44
|
|
|
private string $channelJsIdentifier; |
45
|
|
|
|
46
|
|
|
private AbstractChannelParameters $channelParameters; |
47
|
|
|
|
48
|
|
|
public static function new( |
49
|
|
|
string $channelIdentifier, |
50
|
|
|
AbstractChannelParameters $channelParameters |
51
|
|
|
): self { |
52
|
|
|
$collection = new self(); |
53
|
|
|
$collection |
54
|
|
|
->setChannelIdentifier($channelIdentifier) |
55
|
|
|
->setChannelParameters($channelParameters); |
56
|
|
|
|
57
|
|
|
return $collection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $channelIdentifier |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
private function setChannelIdentifier(string $channelIdentifier): self |
66
|
|
|
{ |
67
|
|
|
$this->channelAuthName = $channelIdentifier; |
68
|
|
|
$this->channelJsVarKey = md5($channelIdentifier); |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param AbstractChannelParameters $channelParameters |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
private function setChannelParameters(AbstractChannelParameters $channelParameters): self |
78
|
|
|
{ |
79
|
|
|
$this->channelParameters = $channelParameters; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the channel identifier for this ChannelEventCollection. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getChannelAuthName(): string |
90
|
|
|
{ |
91
|
|
|
return $this->channelAuthName; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getChannelJsVarKey(): string |
95
|
|
|
{ |
96
|
|
|
return $this->channelJsVarKey; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the channels authentication callback to verify user access. |
101
|
|
|
* |
102
|
|
|
* @return callable |
103
|
|
|
*/ |
104
|
|
|
public function getChannelAuthCallback(): callable |
105
|
|
|
{ |
106
|
|
|
return $this->channelParameters->channelAuthCallback; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the channels authentication callback to verify user access. |
111
|
|
|
* |
112
|
|
|
* @return null|array |
113
|
|
|
*/ |
114
|
|
|
public function getChannelAuthOptions(): ?array |
115
|
|
|
{ |
116
|
|
|
if (0 !== count($this->channelParameters->channelAuthOptions)) { |
117
|
|
|
return $this->channelParameters->channelAuthOptions; |
118
|
|
|
} |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return callable|Closure|string |
124
|
|
|
*/ |
125
|
|
|
public function getChannelContextBindingCallback() |
126
|
|
|
{ |
127
|
|
|
return $this->channelParameters->channelContextBindingCallback; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the string that identifies the echo channel in Javascript. |
132
|
|
|
* |
133
|
|
|
* This will return a JS "Template literal" string. |
134
|
|
|
* It should allow for the specific user's channel's to be dynamically resolved. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
* @example '`App.Models.User.${window.dynamicEchoOld.userID}`' |
138
|
|
|
*/ |
139
|
|
|
public function getChannelJsIdentifier(): string |
140
|
|
|
{ |
141
|
|
|
return $this->channelParameters->channelJsIdentifier; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|