|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EdwinLuijten\Ekko\Broadcast\Broadcasters; |
|
4
|
|
|
|
|
5
|
|
|
use EdwinLuijten\Ekko\Broadcast\Identity; |
|
6
|
|
|
use EdwinLuijten\Ekko\Broadcast\StrUtil; |
|
7
|
|
|
use Predis\ClientInterface; |
|
8
|
|
|
|
|
9
|
|
|
class RedisBroadcaster extends AbstractBroadcaster implements BroadcasterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var ClientInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private $redis; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* RedisBroadcaster constructor. |
|
18
|
6 |
|
* @param ClientInterface $redis |
|
19
|
|
|
*/ |
|
20
|
6 |
|
public function __construct(ClientInterface $redis) |
|
21
|
6 |
|
{ |
|
22
|
|
|
$this->redis = $redis; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Identity $identity |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
public function auth(Identity $identity) |
|
31
|
3 |
|
{ |
|
32
|
|
|
if (StrUtil::startsWith($identity->channel, ['private-', 'presence-']) && empty($identity->identifier)) { |
|
33
|
3 |
|
throw new \Exception('Unauthorized', 403); |
|
34
|
|
|
} |
|
35
|
3 |
|
|
|
36
|
3 |
|
return parent::verifyThatIdentityCanAccessChannel( |
|
|
|
|
|
|
37
|
2 |
|
$identity, |
|
38
|
3 |
|
str_replace(['private-', 'presence-'], '', $identity->channel) |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Identity $identity |
|
44
|
|
|
* @param $response |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function validAuthenticationResponse(Identity $identity, $response) |
|
48
|
|
|
{ |
|
49
|
|
|
if (is_bool($response)) { |
|
50
|
|
|
return json_encode($response); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return json_encode( |
|
54
|
|
|
[ |
|
55
|
|
|
'channel_data' => [ |
|
56
|
|
|
'identifier' => $identity->identifier, |
|
57
|
|
|
'identity' => $response, |
|
58
|
|
|
] |
|
59
|
|
|
] |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Broadcast the given event. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $channels |
|
67
|
|
|
* @param string $event |
|
68
|
|
|
* @param array $payload |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
|
72
|
|
|
{ |
|
73
|
|
|
$socket = isset($payload['socket']) ? $payload['socket'] : null; |
|
74
|
|
|
$payload = json_encode(['event' => $event, 'data' => $payload, 'socket' => $socket]); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($this->formatChannels($channels) as $channel) { |
|
77
|
|
|
$this->redis->publish($channel, $payload); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.