PusherChannelProtocolMessage::ping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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