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

PusherChannelProtocolMessage::unsubscribe()   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
39
    /**
40
     * Subscribe to channel.
41
     *
42
     * @see    https://pusher.com/docs/pusher_protocol#pusher-subscribe
43
     * @param  \Ratchet\ConnectionInterface  $connection
44
     * @param  \stdClass  $payload
45
     * @return void
46
     */
47
    protected function subscribe(ConnectionInterface $connection, stdClass $payload)
48
    {
49
        $this->channelManager->subscribeToChannel($connection, $payload->channel, $payload);
50
    }
51
52
    /**
53
     * Unsubscribe from the channel.
54
     *
55
     * @param  \Ratchet\ConnectionInterface  $connection
56
     * @param  \stdClass  $payload
57
     * @return void
58
     */
59
    public function unsubscribe(ConnectionInterface $connection, stdClass $payload)
60
    {
61
        $this->channelManager->unsubscribeFromChannel($connection, $payload->channel, $payload);
62
    }
63
}
64