Completed
Pull Request — master (#447)
by Alexandru
01:32
created

PusherChannelProtocolMessage::subscribe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Messages;
4
5
use Illuminate\Support\Str;
6
use Ratchet\ConnectionInterface;
7
use stdClass;
8
9
class PusherChannelProtocolMessage extends PusherClientMessage
10
{
11
    /**
12
     * Respond with the payload.
13
     *
14
     * @return void
15
     */
16
    public function respond()
17
    {
18
        $eventName = Str::camel(Str::after($this->payload->event, ':'));
19
20
        if (method_exists($this, $eventName) && $eventName !== 'respond') {
21
            call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());
22
        }
23
    }
24
25
    /**
26
     * Ping the connection.
27
     *
28
     * @see    https://pusher.com/docs/pusher_protocol#ping-pong
29
     * @param  \Ratchet\ConnectionInterface  $connection
30
     * @return void
31
     */
32
    protected function ping(ConnectionInterface $connection)
33
    {
34
        $connection->send(json_encode([
35
            'event' => 'pusher:pong',
36
        ]));
37
38
        $this->channelManager->connectionPonged($connection);
39
    }
40
41
    /**
42
     * Subscribe to channel.
43
     *
44
     * @see    https://pusher.com/docs/pusher_protocol#pusher-subscribe
45
     * @param  \Ratchet\ConnectionInterface  $connection
46
     * @param  \stdClass  $payload
47
     * @return void
48
     */
49
    protected function subscribe(ConnectionInterface $connection, stdClass $payload)
50
    {
51
        $this->channelManager->subscribeToChannel($connection, $payload->channel, $payload);
52
    }
53
54
    /**
55
     * Unsubscribe from the channel.
56
     *
57
     * @param  \Ratchet\ConnectionInterface  $connection
58
     * @param  \stdClass  $payload
59
     * @return void
60
     */
61
    public function unsubscribe(ConnectionInterface $connection, stdClass $payload)
62
    {
63
        $this->channelManager->unsubscribeFromChannel($connection, $payload->channel, $payload);
64
    }
65
}
66