Completed
Pull Request — master (#181)
by
unknown
01:57
created

WebSocketHandler::onError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\ConnectionsOverCapacity;
18
19
class WebSocketHandler implements MessageComponentInterface
20
{
21
    /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
22
    protected $channelManager;
23
24
    public function __construct(ChannelManager $channelManager)
25
    {
26
        $this->channelManager = $channelManager;
27
    }
28
29
    public function onOpen(ConnectionInterface $connection)
30
    {
31
        $this
32
            ->verifyAppKey($connection)
33
            ->limitConcurrentConnections($connection)
34
            ->generateSocketId($connection)
35
            ->establishConnection($connection);
36
    }
37
38
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
39
    {
40
        $pusherMessage = PusherMessageFactory::createForMessage($message, $connection, $this->channelManager);
41
42
        $pusherMessage->respond();
43
44
        if ($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?

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...
45
            $payload = json_decode($message->getPayload());
46
            if (isset($payload->event)
47
                && in_array($payload->event, $connection->app->dispatchEventsForClientMessages)
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?

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...
48
            ) {
49
                $this->dispatchClientMessageEvent($payload->event, $payload);
50
            }
51
        }
52
    }
53
54
    public function onClose(ConnectionInterface $connection)
55
    {
56
        $this->channelManager->removeFromAllChannels($connection);
57
58
        DashboardLogger::disconnection($connection);
59
60
        StatisticsLogger::disconnection($connection);
61
    }
62
63
    public function onError(ConnectionInterface $connection, Exception $exception)
64
    {
65
        if ($exception instanceof WebSocketException) {
66
            $connection->send(json_encode(
67
                $exception->getPayload()
68
            ));
69
        }
70
    }
71
72
    protected function verifyAppKey(ConnectionInterface $connection)
73
    {
74
        $appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
0 ignored issues
show
Bug introduced by
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...
75
76
        if (! $app = App::findByKey($appKey)) {
77
            throw new UnknownAppKey($appKey);
78
        }
79
80
        $connection->app = $app;
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?

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 limitConcurrentConnections(ConnectionInterface $connection)
86
    {
87
        if (! is_null($capacity = $connection->app->capacity)) {
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?

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...
88
            $connectionsCount = $this->channelManager->getConnectionCount($connection->app->id);
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?

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...
89
            if ($connectionsCount >= $capacity) {
90
                throw new ConnectionsOverCapacity();
91
            }
92
        }
93
94
        return $this;
95
    }
96
97
    protected function generateSocketId(ConnectionInterface $connection)
98
    {
99
        $socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000));
100
101
        $connection->socketId = $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?

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...
102
103
        return $this;
104
    }
105
106
    protected function establishConnection(ConnectionInterface $connection)
107
    {
108
        $connection->send(json_encode([
109
            'event' => 'pusher:connection_established',
110
            'data' => json_encode([
111
                'socket_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?

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...
112
                'activity_timeout' => 30,
113
            ]),
114
        ]));
115
116
        DashboardLogger::connection($connection);
117
118
        StatisticsLogger::connection($connection);
119
120
        return $this;
121
    }
122
123
    protected function dispatchClientMessageEvent(string $event, $payload)
124
    {
125
        app('events')->dispatch('websockets.' . $event, [$payload]);
126
    }
127
}
128