Completed
Pull Request — master (#447)
by Alexandru
01:47
created

WebSocketsLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 6 1
A setApp() 0 6 1
A onOpen() 0 8 1
A onMessage() 0 6 1
A onClose() 0 8 1
A onError() 0 16 2
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);
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 object<Ratchet\WebSocket...sageComponentInterface> is incompatible with the declared type object<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?

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...
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 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...
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...
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?

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...
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?

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
        $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