PusherClientMessage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 65
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A respond() 0 23 3
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Messages;
4
5
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
6
use BeyondCode\LaravelWebSockets\Contracts\PusherMessage;
7
use BeyondCode\LaravelWebSockets\DashboardLogger;
8
use Illuminate\Support\Str;
9
use Ratchet\ConnectionInterface;
10
use stdClass;
11
12
class PusherClientMessage implements PusherMessage
13
{
14
    /**
15
     * The payload to send.
16
     *
17
     * @var \stdClass
18
     */
19
    protected $payload;
20
21
    /**
22
     * The socket connection.
23
     *
24
     * @var \Ratchet\ConnectionInterface
25
     */
26
    protected $connection;
27
28
    /**
29
     * The channel manager.
30
     *
31
     * @var ChannelManager
32
     */
33
    protected $channelManager;
34
35
    /**
36
     * Create a new instance.
37
     *
38
     * @param  \stdClass  $payload
39
     * @param  \Ratchet\ConnectionInterface  $connection
40
     * @param  ChannelManager  $channelManager
41
     */
42
    public function __construct(stdClass $payload, ConnectionInterface $connection, ChannelManager $channelManager)
43
    {
44
        $this->payload = $payload;
45
        $this->connection = $connection;
46
        $this->channelManager = $channelManager;
47
    }
48
49
    /**
50
     * Respond to the message construction.
51
     *
52
     * @return void
53
     */
54
    public function respond()
55
    {
56
        if (! Str::startsWith($this->payload->event, 'client-')) {
57
            return;
58
        }
59
60
        if (! $this->connection->app->clientMessagesEnabled) {
0 ignored issues
show
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...
61
            return;
62
        }
63
64
        $channel = $this->channelManager->find(
65
            $this->connection->app->id, $this->payload->channel
66
        );
67
68
        optional($channel)->broadcastToEveryoneExcept(
69
            $this->payload, $this->connection->socketId, $this->connection->app->id
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...
70
        );
71
72
        DashboardLogger::log($this->connection->app->id, DashboardLogger::TYPE_WS_MESSAGE, [
73
            'socketId' => $this->connection->socketId,
74
            'event' => $this->payload->event,
75
            'channel' => $this->payload->channel,
76
            'data' => $this->payload,
77
        ]);
78
    }
79
}
80