-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAnonymousEvent.php
151 lines (126 loc) · 3.32 KB
/
AnonymousEvent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Illuminate\Broadcasting;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class AnonymousEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithBroadcasting, InteractsWithSockets;
/**
* The connection the event should be broadcast on.
*/
protected ?string $connection = null;
/**
* The name the event should be broadcast as.
*/
protected ?string $name = null;
/**
* The payload the event should be broadcast with.
*/
protected array $payload = [];
/**
* Should the broadcast include the current user.
*/
protected bool $includeCurrentUser = true;
/**
* Indicates if the event should be broadcast synchronously.
*/
protected bool $shouldBroadcastNow = false;
/**
* Create a new anonymous broadcastable event instance.
*
* @return void
*/
public function __construct(protected Channel|array|string $channels)
{
$this->channels = Arr::wrap($channels);
}
/**
* Set the connection the event should be broadcast on.
*/
public function via(string $connection): static
{
$this->connection = $connection;
return $this;
}
/**
* Set the name the event should be broadcast as.
*/
public function as(string $name): static
{
$this->name = $name;
return $this;
}
/**
* Set the payload the event should be broadcast with.
*/
public function with(Arrayable|array $payload): static
{
$this->payload = $payload instanceof Arrayable
? $payload->toArray()
: (new Collection($payload))->map(
fn ($p) => $p instanceof Arrayable ? $p->toArray() : $p
)->all();
return $this;
}
/**
* Broadcast the event to everyone except the current user.
*/
public function toOthers(): static
{
$this->includeCurrentUser = false;
return $this;
}
/**
* Broadcast the event.
*/
public function sendNow(): void
{
$this->shouldBroadcastNow = true;
$this->send();
}
/**
* Broadcast the event.
*/
public function send(): void
{
$broadcast = broadcast($this)->via($this->connection);
if (! $this->includeCurrentUser) {
$broadcast->toOthers();
}
}
/**
* Get the name the event should broadcast as.
*/
public function broadcastAs(): string
{
return $this->name ?: class_basename($this);
}
/**
* Get the payload the event should broadcast with.
*
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return $this->payload;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]|string[]|string
*/
public function broadcastOn(): Channel|array
{
return $this->channels;
}
/**
* Determine if the event should be broadcast synchronously.
*/
public function shouldBroadcastNow(): bool
{
return $this->shouldBroadcastNow;
}
}