Completed
Pull Request — master (#447)
by Alexandru
10:08
created

LocalClient::subscribeToApp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\PubSub\Drivers;
4
5
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
6
use React\EventLoop\LoopInterface;
7
use React\Promise\FulfilledPromise;
8
use React\Promise\PromiseInterface;
9
use stdClass;
10
11
class LocalClient implements ReplicationInterface
12
{
13
    /**
14
     * Mapping of the presence JSON data for users in each channel.
15
     *
16
     * @var string[][]
17
     */
18
    protected $channelData = [];
19
20
    /**
21
     * Boot the pub/sub provider (open connections, initial subscriptions, etc).
22
     *
23
     * @param  LoopInterface  $loop
24
     * @param  string|null  $factoryClass
25
     * @return self
26
     */
27
    public function boot(LoopInterface $loop, $factoryClass = null): ReplicationInterface
28
    {
29
        return $this;
30
    }
31
32
    /**
33
     * Publish a payload on a specific channel, for a specific app.
34
     *
35
     * @param  string  $appId
36
     * @param  string  $channel
37
     * @param  stdClass  $payload
38
     * @return bool
39
     */
40
    public function publish($appId, string $channel, stdClass $payload): bool
41
    {
42
        return true;
43
    }
44
45
    /**
46
     * Subscribe to receive messages for a channel.
47
     *
48
     * @param  string  $appId
49
     * @param  string  $channel
50
     * @return bool
51
     */
52
    public function subscribe($appId, string $channel): bool
53
    {
54
        return true;
55
    }
56
57
    /**
58
     * Unsubscribe from a channel.
59
     *
60
     * @param  string  $appId
61
     * @param  string  $channel
62
     * @return bool
63
     */
64
    public function unsubscribe($appId, string $channel): bool
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * Subscribe to the app's pubsub keyspace.
71
     *
72
     * @param  mixed  $appId
73
     * @return bool
74
     */
75
    public function subscribeToApp($appId): bool
76
    {
77
        return true;
78
    }
79
80
    /**
81
     * Unsubscribe from the app's pubsub keyspace.
82
     *
83
     * @param  mixed  $appId
84
     * @return bool
85
     */
86
    public function unsubscribeFromApp($appId): bool
87
    {
88
        return true;
89
    }
90
91
    /**
92
     * Add a member to a channel. To be called when they have
93
     * subscribed to the channel.
94
     *
95
     * @param  string  $appId
96
     * @param  string  $channel
97
     * @param  string  $socketId
98
     * @param  string  $data
99
     * @return void
100
     */
101
    public function joinChannel($appId, string $channel, string $socketId, string $data)
102
    {
103
        $this->channelData["{$appId}:{$channel}"][$socketId] = $data;
104
    }
105
106
    /**
107
     * Remove a member from the channel. To be called when they have
108
     * unsubscribed from the channel.
109
     *
110
     * @param  string  $appId
111
     * @param  string  $channel
112
     * @param  string  $socketId
113
     * @return void
114
     */
115
    public function leaveChannel($appId, string $channel, string $socketId)
116
    {
117
        unset($this->channelData["{$appId}:{$channel}"][$socketId]);
118
119
        if (empty($this->channelData["{$appId}:{$channel}"])) {
120
            unset($this->channelData["{$appId}:{$channel}"]);
121
        }
122
    }
123
124
    /**
125
     * Retrieve the full information about the members in a presence channel.
126
     *
127
     * @param  string  $appId
128
     * @param  string  $channel
129
     * @return PromiseInterface
130
     */
131
    public function channelMembers($appId, string $channel): PromiseInterface
132
    {
133
        $members = $this->channelData["{$appId}:{$channel}"] ?? [];
134
135
        $members = array_map(function ($user) {
136
            return json_decode($user);
137
        }, $members);
138
139
        return new FulfilledPromise($members);
0 ignored issues
show
Deprecated Code introduced by
The class React\Promise\FulfilledPromise has been deprecated with message: 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
140
    }
141
142
    /**
143
     * Get the amount of users subscribed for each presence channel.
144
     *
145
     * @param  string  $appId
146
     * @param  array  $channelNames
147
     * @return PromiseInterface
148
     */
149
    public function channelMemberCounts($appId, array $channelNames): PromiseInterface
150
    {
151
        $results = [];
152
153
        // Count the number of users per channel
154
        foreach ($channelNames as $channel) {
155
            $results[$channel] = isset($this->channelData["{$appId}:{$channel}"])
156
                ? count($this->channelData["{$appId}:{$channel}"])
157
                : 0;
158
        }
159
160
        return new FulfilledPromise($results);
0 ignored issues
show
Deprecated Code introduced by
The class React\Promise\FulfilledPromise has been deprecated with message: 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
161
    }
162
163
    /**
164
     * Get the amount of unique connections.
165
     *
166
     * @param  mixed  $appId
167
     * @return null|int
168
     */
169
    public function getLocalConnectionsCount($appId)
170
    {
171
        return null;
172
    }
173
174
    /**
175
     * Get the amount of connections aggregated on multiple instances.
176
     *
177
     * @param  mixed  $appId
178
     * @return null|int|\React\Promise\PromiseInterface
179
     */
180
    public function getGlobalConnectionsCount($appId)
181
    {
182
        return null;
183
    }
184
}
185