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

PresenceChannel::unsubscribe()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 2
nc 1
nop 1
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
                $connection->send(json_encode([
38
                    'event' => 'pusher_internal:subscription_succeeded',
39
                    'channel' => $this->getName(),
40
                    'data' => json_encode($this->getChannelData($users)),
41
                ]));
42
            });
43
44
        $memberAddedPayload = [
45
            'event' => 'pusher_internal:member_added',
46
            'channel' => $this->getName(),
47
            'data' => $payload->channel_data,
48
        ];
49
50
        $this->broadcastToEveryoneExcept(
51
            (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...
52
            $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...
53
        );
54
55
        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...
56
            '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...
57
            'channel' => $this->getName(),
58
        ]);
59
    }
60
61
    /**
62
     * Unsubscribe connection from the channel.
63
     *
64
     * @param  \Ratchet\ConnectionInterface  $connection
65
     * @return void
66
     */
67
    public function unsubscribe(ConnectionInterface $connection)
68
    {
69
        parent::unsubscribe($connection);
70
71
        $this->channelManager
72
            ->getChannelMember($connection, $this->getName())
73
            ->then(function ($user) use ($connection) {
74
                $user = @json_decode($user);
75
76
                if (! $user) {
77
                    return;
78
                }
79
80
                $this->channelManager->userLeftPresenceChannel(
81
                    $connection, $user, $this->getName()
82
                );
83
84
                $memberRemovedPayload = [
85
                    'event' => 'pusher_internal:member_removed',
86
                    'channel' => $this->getName(),
87
                    'data' => json_encode([
88
                        'user_id' => $user->user_id,
89
                    ]),
90
                ];
91
92
                $this->broadcastToEveryoneExcept(
93
                    (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...
94
                    $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...
95
                );
96
            });
97
    }
98
99
    /**
100
     * Get the Presence channel data.
101
     *
102
     * @param  array  $users
103
     * @return array
104
     */
105
    protected function getChannelData(array $users): array
106
    {
107
        return [
108
            'presence' => [
109
                'ids' => $this->getUserIds($users),
110
                'hash' => $this->getHash($users),
111
                'count' => count($users),
112
            ],
113
        ];
114
    }
115
116
    /**
117
     * Get the Presence Channel's users.
118
     *
119
     * @param  array  $users
120
     * @return array
121
     */
122
    protected function getUserIds(array $users): array
123
    {
124
        return collect($users)
125
            ->map(function ($user) {
126
                return (string) $user->user_id;
127
            })
128
            ->values();
129
    }
130
131
    /**
132
     * Compute the hash for the presence channel integrity.
133
     *
134
     * @param  array  $users
135
     * @return array
136
     */
137
    protected function getHash(array $users): array
138
    {
139
        $hash = [];
140
141
        foreach ($users as $socketId => $user) {
142
            $hash[$user->user_id] = $user->user_info ?? [];
143
        }
144
145
        return $hash;
146
    }
147
}
148