1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Pusher; |
4
|
|
|
|
5
|
|
|
final class Event implements \JsonSerializable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $event; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $channel; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $event |
24
|
|
|
* @param array $data |
25
|
|
|
* @param string $channel |
26
|
|
|
*/ |
27
|
15 |
|
public function __construct(string $event, array $data, string $channel = '') |
28
|
|
|
{ |
29
|
15 |
|
$this->event = $event; |
30
|
15 |
|
$this->data = $data; |
31
|
15 |
|
$this->channel = $channel; |
32
|
15 |
|
} |
33
|
|
|
|
34
|
15 |
|
public static function createFromMessage(array $message): self |
35
|
|
|
{ |
36
|
15 |
|
return new self( |
37
|
15 |
|
$message['event'], |
38
|
15 |
|
is_array($message['data']) ? $message['data'] : json_decode($message['data'], true), |
39
|
15 |
|
isset($message['channel']) ? $message['channel'] : '' |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
public function jsonSerialize() |
44
|
|
|
{ |
45
|
7 |
|
return json_encode(['event' => $this->event, 'data' => $this->data, 'channel' => $this->channel]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
15 |
|
public function getEvent() |
52
|
|
|
{ |
53
|
15 |
|
return $this->event; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
15 |
|
public function getChannel() |
60
|
|
|
{ |
61
|
15 |
|
return $this->channel; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
15 |
|
public function getData() |
68
|
|
|
{ |
69
|
15 |
|
return $this->data; |
70
|
|
|
} |
71
|
|
|
|
72
|
11 |
|
public static function isError(Event $event): bool |
73
|
|
|
{ |
74
|
11 |
|
return $event->getEvent() === 'pusher:error'; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
public static function subscriptionSucceeded(Event $event): bool |
78
|
|
|
{ |
79
|
8 |
|
return $event->getEvent() !== 'pusher_internal:subscription_succeeded'; |
80
|
|
|
} |
81
|
|
|
|
82
|
11 |
|
public static function connectionEstablished(Event $event): bool |
83
|
|
|
{ |
84
|
11 |
|
return $event->getEvent() === 'pusher:connection_established'; |
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
public static function subscribeOn(string $channel): array |
88
|
|
|
{ |
89
|
11 |
|
return ['event' => 'pusher:subscribe', 'data' => ['channel' => $channel]]; |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
public static function unsubscribeOn(string $channel): array |
93
|
|
|
{ |
94
|
14 |
|
return ['event' => 'pusher:unsubscribe', 'data' => ['channel' => $channel]]; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public static function ping(): array |
98
|
|
|
{ |
99
|
2 |
|
return ['event' => 'pusher:ping']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|