Completed
Pull Request — master (#447)
by Marcel
03:00 queued 01:29
created

PresenceChannel::subscribe()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 8.7853
c 0
b 0
f 0
cc 3
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Channels;
4
5
use BeyondCode\LaravelWebSockets\DashboardLogger;
6
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
7
use Ratchet\ConnectionInterface;
8
use stdClass;
9
10
class PresenceChannel extends PrivateChannel
11
{
12
    /**
13
     * Subscribe to the channel.
14
     *
15
     * @see    https://pusher.com/docs/pusher_protocol#presence-channel-events
16
     * @param  \Ratchet\ConnectionInterface  $connection
17
     * @param  \stdClass  $payload
18
     * @return void
19
     * @throws InvalidSignature
20
     */
21
    public function subscribe(ConnectionInterface $connection, stdClass $payload)
22
    {
23
        $this->verifySignature($connection, $payload);
24
25
        $this->saveConnection($connection);
26
27
        $this->channelManager->userJoinedPresenceChannel(
0 ignored issues
show
Bug introduced by
The property channelManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
            $connection,
29
            $user = json_decode($payload->channel_data),
30
            $this->getName(),
31
            $payload
32
        );
33
34
        $this->channelManager
35
            ->getChannelMembers($connection->app->id, $this->getName())
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...
36
            ->then(function ($users) use ($connection) {
37
                $hash = [];
38
39
                foreach ($users as $socketId => $user) {
40
                    $hash[$user->user_id] = $user->user_info ?? [];
41
                }
42
43
                $connection->send(json_encode([
44
                    'event' => 'pusher_internal:subscription_succeeded',
45
                    'channel' => $this->getName(),
46
                    'data' => json_encode([
47
                        'presence' => [
48
                            'ids' => collect($users)->map(function ($user) {
49
                                return (string) $user->user_id;
50
                            })->values(),
51
                            'hash' => $hash,
52
                            'count' => count($users),
53
                        ],
54
                    ]),
55
                ]));
56
            });
57
58
        // The `pusher_internal:member_added` event is triggered when a user joins a channel.
59
        // It's quite possible that a user can have multiple connections to the same channel
60
        // (for example by having multiple browser tabs open)
61
        // and in this case the events will only be triggered when the first tab is opened.
62
        $this->channelManager
63
            ->getMemberSockets($user->user_id, $connection->app->id, $this->getName())
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...
64
            ->then(function ($sockets) use ($payload, $connection) {
65
                if (count($sockets) === 1) {
66
                    $memberAddedPayload = [
67
                        'event' => 'pusher_internal:member_added',
68
                        'channel' => $this->getName(),
69
                        'data' => $payload->channel_data,
70
                    ];
71
72
                    $this->broadcastToEveryoneExcept(
73
                        (object) $memberAddedPayload, $connection->socketId,
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...
74
                        $connection->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...
75
                    );
76
                }
77
78
                DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_SUBSCRIBED, [
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...
79
                    'socketId' => $connection->socketId,
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...
80
                    'channel' => $this->getName(),
81
                    'duplicate-connection' => count($sockets) > 1,
82
                ]);
83
            });
84
    }
85
86
    /**
87
     * Unsubscribe connection from the channel.
88
     *
89
     * @param  \Ratchet\ConnectionInterface  $connection
90
     * @return void
91
     */
92
    public function unsubscribe(ConnectionInterface $connection)
93
    {
94
        parent::unsubscribe($connection);
95
96
        $this->channelManager
97
            ->getChannelMember($connection, $this->getName())
98
            ->then(function ($user) use ($connection) {
99
                $user = @json_decode($user);
100
101
                if (! $user) {
102
                    return;
103
                }
104
105
                $this->channelManager->userLeftPresenceChannel(
106
                    $connection, $user, $this->getName()
107
                );
108
109
                // The `pusher_internal:member_removed` is triggered when a user leaves a channel.
110
                // It's quite possible that a user can have multiple connections to the same channel
111
                // (for example by having multiple browser tabs open)
112
                // and in this case the events will only be triggered when the last one is closed.
113
                $this->channelManager
114
                    ->getMemberSockets($user->user_id, $connection->app->id, $this->getName())
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...
115
                    ->then(function ($sockets) use ($connection, $user) {
116
                        if (count($sockets) === 0) {
117
                            $memberRemovedPayload = [
118
                                'event' => 'pusher_internal:member_removed',
119
                                'channel' => $this->getName(),
120
                                'data' => json_encode([
121
                                    'user_id' => $user->user_id,
122
                                ]),
123
                            ];
124
125
                            $this->broadcastToEveryoneExcept(
126
                                (object) $memberRemovedPayload, $connection->socketId,
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...
127
                                $connection->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...
128
                            );
129
                        }
130
                    });
131
            });
132
    }
133
}
134