DashboardLogger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 84
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A connection() 0 10 1
A occupied() 0 6 1
A subscribed() 0 7 1
A clientMessage() 0 8 1
A disconnection() 0 6 1
A vacated() 0 6 1
A apiMessage() 0 7 1
A log() 0 15 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Dashboard;
4
5
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
6
use Ratchet\ConnectionInterface;
7
use stdClass;
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
Bug introduced by Marcel Pociot
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
Bug introduced by freek
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
Bug introduced by Marcel Pociot
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
Bug introduced by freek
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
Bug introduced by freek
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
Bug introduced by freek
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
Bug introduced by freek
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
Bug introduced by freek
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
Bug introduced by freek
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
Bug introduced by Marcel Pociot
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
Bug introduced by freek
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