Completed
Push — master ( 53691c...15e6b6 )
by Marcel
01:44
created

src/Dashboard/DashboardLogger.php (11 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\Dashboard;
4
5
use stdClass;
6
use Ratchet\ConnectionInterface;
7
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
8
9
class DashboardLogger
10
{
11
    const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-';
12
    const TYPE_DISCONNECTION = 'disconnection';
13
    const TYPE_CONNECTION = 'connection';
14
    const TYPE_VACATED = 'vacated';
15
    const TYPE_OCCUPIED = 'occupied';
16
    const TYPE_SUBSCRIBED = 'subscribed';
17
    const TYPE_CLIENT_MESSAGE = 'client-message';
18
    const TYPE_API_MESSAGE = 'api-message';
19
20
    public static function connection(ConnectionInterface $connection)
21
    {
22
        /** @var \GuzzleHttp\Psr7\Request $request */
23
        $request = $connection->httpRequest;
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...
24
25
        static::log($connection->app->id, static::TYPE_CONNECTION, [
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...
26
            'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}",
27
            'socketId' => $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...
28
        ]);
29
    }
30
31
    public static function occupied(ConnectionInterface $connection, string $channelName)
32
    {
33
        static::log($connection->app->id, static::TYPE_OCCUPIED, [
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...
34
            'details' => "Channel: {$channelName}",
35
        ]);
36
    }
37
38
    public static function subscribed(ConnectionInterface $connection, string $channelName)
39
    {
40
        static::log($connection->app->id, static::TYPE_SUBSCRIBED, [
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...
41
            'socketId' => $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...
42
            'details' => "Channel: {$channelName}",
43
        ]);
44
    }
45
46
    public static function clientMessage(ConnectionInterface $connection, stdClass $payload)
47
    {
48
        static::log($connection->app->id, static::TYPE_CLIENT_MESSAGE, [
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...
49
            'details' => "Channel: {$payload->channel}, Event: {$payload->event}",
50
            'socketId' => $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...
51
            'data' => json_encode($payload),
52
        ]);
53
    }
54
55
    public static function disconnection(ConnectionInterface $connection)
56
    {
57
        static::log($connection->app->id, static::TYPE_DISCONNECTION, [
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...
58
            'socketId' => $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...
59
        ]);
60
    }
61
62
    public static function vacated(ConnectionInterface $connection, string $channelName)
63
    {
64
        static::log($connection->app->id, static::TYPE_VACATED, [
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...
65
            'details' => "Channel: {$channelName}",
66
        ]);
67
    }
68
69
    public static function apiMessage($appId, string $channel, string $event, string $payload)
70
    {
71
        static::log($appId, static::TYPE_API_MESSAGE, [
72
            'details' => "Channel: {$channel}, Event: {$event}",
73
            'data' => $payload,
74
        ]);
75
    }
76
77
    public static function log($appId, string $type, array $attributes = [])
78
    {
79
        $channelName = static::LOG_CHANNEL_PREFIX.$type;
80
81
        $channel = app(ChannelManager::class)->find($appId, $channelName);
82
83
        optional($channel)->broadcast([
84
            'event' => 'log-message',
85
            'channel' => $channelName,
86
            'data' => [
87
                'type' => $type,
88
                'time' => strftime('%H:%M:%S'),
89
            ] + $attributes,
90
        ]);
91
    }
92
}
93