Completed
Pull Request — master (#447)
by Alexandru
15:06 queued 01:45
created

PusherChannelProtocolMessage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A respond() 0 8 3
A ping() 0 6 1
A subscribe() 0 4 1
A unsubscribe() 0 4 1
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