WebSocketsLogger   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 101
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A onError() 0 15 2
A setApp() 0 5 1
A onMessage() 0 5 1
A decorate() 0 5 1
A onClose() 0 7 1
A onOpen() 0 7 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
4
5
use BeyondCode\LaravelWebSockets\Server\QueryParameters;
6
use Exception;
7
use Ratchet\ConnectionInterface;
8
use Ratchet\RFC6455\Messaging\MessageInterface;
9
use Ratchet\WebSocket\MessageComponentInterface;
10
11
class WebSocketsLogger extends Logger implements MessageComponentInterface
12
{
13
    /**
14
     * The HTTP app instance to watch.
15
     *
16
     * @var \Ratchet\Http\HttpServerInterface
17
     */
18
    protected $app;
19
20
    /**
21
     * Create a new instance and add the app to watch.
22
     *
23
     * @param  \Ratchet\MessageComponentInterface  $app
24
     * @return self
25
     */
26
    public static function decorate(MessageComponentInterface $app): self
27
    {
28
        $logger = clone app(self::class);
29
30
        return $logger->setApp($app);
0 ignored issues
show
Bug introduced by
The method setApp() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return $logger->/** @scrutinizer ignore-call */ setApp($app);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
    }
32
33
    /**
34
     * Set a new app to watch.
35
     *
36
     * @param  \Ratchet\MessageComponentInterface  $app
37
     * @return $this
38
     */
39
    public function setApp(MessageComponentInterface $app)
40
    {
41
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type Ratchet\WebSocket\MessageComponentInterface is incompatible with the declared type Ratchet\Http\HttpServerInterface of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
43
        return $this;
44
    }
45
46
    /**
47
     * Handle the HTTP open request.
48
     *
49
     * @param  \Ratchet\ConnectionInterface  $connection
50
     * @return void
51
     */
52
    public function onOpen(ConnectionInterface $connection)
53
    {
54
        $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?
Loading history...
55
56
        $this->warn("New connection opened for app key {$appKey}.");
57
58
        $this->app->onOpen(ConnectionLogger::decorate($connection));
59
    }
60
61
    /**
62
     * Handle the HTTP message request.
63
     *
64
     * @param  \Ratchet\ConnectionInterface  $connection
65
     * @param  \Ratchet\RFC6455\Messaging\MessageInterface  $message
66
     * @return void
67
     */
68
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
69
    {
70
        $this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
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...
71
72
        $this->app->onMessage(ConnectionLogger::decorate($connection), $message);
73
    }
74
75
    /**
76
     * Handle the HTTP close request.
77
     *
78
     * @param  \Ratchet\ConnectionInterface  $connection
79
     * @return void
80
     */
81
    public function onClose(ConnectionInterface $connection)
82
    {
83
        $socketId = $connection->socketId ?? null;
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...
84
85
        $this->warn("Connection id {$socketId} closed.");
86
87
        $this->app->onClose(ConnectionLogger::decorate($connection));
88
    }
89
90
    /**
91
     * Handle HTTP errors.
92
     *
93
     * @param  \Ratchet\ConnectionInterface  $connection
94
     * @param  Exception  $exception
95
     * @return void
96
     */
97
    public function onError(ConnectionInterface $connection, Exception $exception)
98
    {
99
        $exceptionClass = get_class($exception);
100
101
        $appId = $connection->app->id ?? 'Unknown 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?
Loading history...
102
103
        $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`.";
104
105
        if ($this->verbose) {
106
            $message .= $exception->getTraceAsString();
107
        }
108
109
        $this->error($message);
110
111
        $this->app->onError(ConnectionLogger::decorate($connection), $exception);
112
    }
113
}
114