HttpLogger::decorate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
4
5
use Exception;
6
use Ratchet\ConnectionInterface;
7
use Ratchet\MessageComponentInterface;
8
9
class HttpLogger extends Logger implements MessageComponentInterface
10
{
11
    /**
12
     * The HTTP app instance to watch.
13
     *
14
     * @var \Ratchet\Http\HttpServerInterface
15
     */
16
    protected $app;
17
18
    /**
19
     * Create a new instance and add the app to watch.
20
     *
21
     * @param  \Ratchet\MessageComponentInterface  $app
22
     * @return self
23
     */
24
    public static function decorate(MessageComponentInterface $app): self
25
    {
26
        $logger = app(self::class);
27
28
        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

28
        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...
29
    }
30
31
    /**
32
     * Set a new app to watch.
33
     *
34
     * @param  \Ratchet\MessageComponentInterface  $app
35
     * @return $this
36
     */
37
    public function setApp(MessageComponentInterface $app)
38
    {
39
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
$app is of type Ratchet\MessageComponentInterface, but the property $app was declared to be of type Ratchet\Http\HttpServerInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
40
41
        return $this;
42
    }
43
44
    /**
45
     * Handle the HTTP open request.
46
     *
47
     * @param  \Ratchet\ConnectionInterface  $connection
48
     * @return void
49
     */
50
    public function onOpen(ConnectionInterface $connection)
51
    {
52
        $this->app->onOpen($connection);
53
    }
54
55
    /**
56
     * Handle the HTTP message request.
57
     *
58
     * @param  \Ratchet\ConnectionInterface  $connection
59
     * @param  mixed  $message
60
     * @return void
61
     */
62
    public function onMessage(ConnectionInterface $connection, $message)
63
    {
64
        $this->app->onMessage($connection, $message);
65
    }
66
67
    /**
68
     * Handle the HTTP close request.
69
     *
70
     * @param  \Ratchet\ConnectionInterface  $connection
71
     * @return void
72
     */
73
    public function onClose(ConnectionInterface $connection)
74
    {
75
        $this->app->onClose($connection);
76
    }
77
78
    /**
79
     * Handle HTTP errors.
80
     *
81
     * @param  \Ratchet\ConnectionInterface  $connection
82
     * @param  Exception  $exception
83
     * @return void
84
     */
85
    public function onError(ConnectionInterface $connection, Exception $exception)
86
    {
87
        $exceptionClass = get_class($exception);
88
89
        $message = "Exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
90
91
        if ($this->verbose) {
92
            $message .= $exception->getTraceAsString();
93
        }
94
95
        $this->error($message);
96
97
        $this->app->onError($connection, $exception);
98
    }
99
}
100