Completed
Push — master ( edcf20...3ec6c0 )
by Marcel
03:05 queued 01:22
created

src/WebSockets/WebSocketHandler.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\WebSockets;
4
5
use Exception;
6
use Ratchet\ConnectionInterface;
7
use BeyondCode\LaravelWebSockets\Apps\App;
8
use Ratchet\RFC6455\Messaging\MessageInterface;
9
use Ratchet\WebSocket\MessageComponentInterface;
10
use BeyondCode\LaravelWebSockets\QueryParameters;
11
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
12
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
13
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
14
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
15
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\WebSocketException;
16
use BeyondCode\LaravelWebSockets\WebSockets\Messages\PusherMessageFactory;
17
18
class WebSocketHandler implements MessageComponentInterface
19
{
20
    /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
21
    protected $channelManager;
22
23
    public function __construct(ChannelManager $channelManager)
24
    {
25
        $this->channelManager = $channelManager;
26
    }
27
28
    public function onOpen(ConnectionInterface $connection)
29
    {
30
        $this
31
            ->verifyAppKey($connection)
32
            ->generateSocketId($connection)
33
            ->establishConnection($connection);
34
    }
35
36
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
37
    {
38
        $message = PusherMessageFactory::createForMessage($message, $connection, $this->channelManager);
39
40
        $message->respond();
41
42
        StatisticsLogger::webSocketMessage($connection);
43
    }
44
45
    public function onClose(ConnectionInterface $connection)
46
    {
47
        $this->channelManager->removeFromAllChannels($connection);
48
49
        DashboardLogger::disconnection($connection);
50
51
        StatisticsLogger::disconnection($connection);
52
    }
53
54
    public function onError(ConnectionInterface $connection, Exception $exception)
55
    {
56
        if ($exception instanceof WebSocketException) {
57
            $connection->send(json_encode(
58
                $exception->getPayload()
59
            ));
60
        }
61
    }
62
63
    protected function verifyAppKey(ConnectionInterface $connection)
64
    {
65
        $appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
0 ignored issues
show
Accessing httpRequest on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66
67
        if (! $app = App::findByKey($appKey)) {
68
            throw new UnknownAppKey($appKey);
69
        }
70
71
        $connection->app = $app;
0 ignored issues
show
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
73
        return $this;
74
    }
75
76
    protected function generateSocketId(ConnectionInterface $connection)
77
    {
78
        $socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000));
79
80
        $connection->socketId = $socketId;
0 ignored issues
show
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
82
        return $this;
83
    }
84
85
    protected function establishConnection(ConnectionInterface $connection)
86
    {
87
        $connection->send(json_encode([
88
            'event' => 'pusher:connection_established',
89
            'data' => json_encode([
90
                'socket_id' => $connection->socketId,
0 ignored issues
show
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
91
                'activity_timeout' => 30,
92
            ]),
93
        ]));
94
95
        DashboardLogger::connection($connection);
96
97
        StatisticsLogger::connection($connection);
98
99
        return $this;
100
    }
101
}
102