Passed
Pull Request — master (#1028)
by
unknown
10:40
created

LocalChannelManager::getChannelsSocketsCount()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\ChannelManagers;
4
5
use BeyondCode\LaravelWebSockets\Channels\Channel;
6
use BeyondCode\LaravelWebSockets\Channels\PresenceChannel;
7
use BeyondCode\LaravelWebSockets\Channels\PrivateChannel;
8
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
9
use BeyondCode\LaravelWebSockets\Helpers;
10
use Carbon\Carbon;
11
use Illuminate\Cache\ArrayLock;
12
use Illuminate\Cache\ArrayStore;
13
use Illuminate\Support\Str;
14
use Ratchet\ConnectionInterface;
15
use React\EventLoop\LoopInterface;
16
use React\Promise\PromiseInterface;
17
use stdClass;
18
19
class LocalChannelManager implements ChannelManager
20
{
21
    /**
22
     * The list of stored channels.
23
     *
24
     * @var array
25
     */
26
    protected $channels = [];
27
28
    /**
29
     * The list of channels by socket and their attached id.
30
     *
31
     * @var array
32
     */
33
    protected $channelSockets = [];
34
35
    /**
36
     * The list of users that joined the presence channel.
37
     *
38
     * @var array
39
     */
40
    protected $users = [];
41
42
    /**
43
     * The list of users by socket and their attached id.
44
     *
45
     * @var array
46
     */
47
    protected $userSockets = [];
48
49
    /**
50
     * Wether the current instance accepts new connections.
51
     *
52
     * @var bool
53
     */
54
    protected $acceptsNewConnections = true;
55
56
    /**
57
     * The ArrayStore instance of locks.
58
     *
59
     * @var \Illuminate\Cache\ArrayStore
60
     */
61
    protected $store;
62
63
    /**
64
     * The unique server identifier.
65
     *
66
     * @var string
67
     */
68
    protected $serverId;
69
70
    /**
71
     * The lock name to use on Array to avoid multiple
72
     * actions that might lead to multiple processings.
73
     *
74
     * @var string
75
     */
76
    protected static $lockName = 'laravel-websockets:channel-manager:lock';
77
78
    /**
79
     * Create a new channel manager instance.
80
     *
81
     * @param  LoopInterface  $loop
82
     * @param  string|null  $factoryClass
83
     * @return void
84
     */
85
    public function __construct(LoopInterface $loop, $factoryClass = null)
86
    {
87
        $this->store = new ArrayStore;
88
        $this->serverId = Str::uuid()->toString();
89
    }
90
91
    /**
92
     * Find the channel by app & name.
93
     *
94
     * @param  string|int  $appId
95
     * @param  string  $channel
96
     * @return null|BeyondCode\LaravelWebSockets\Channels\Channel
0 ignored issues
show
Bug introduced by
The type BeyondCode\LaravelWebSoc...ockets\Channels\Channel was not found. Did you mean BeyondCode\LaravelWebSockets\Channels\Channel? If so, make sure to prefix the type with \.
Loading history...
97
     */
98
    public function find($appId, string $channel)
99
    {
100
        return $this->channels[$appId][$channel] ?? null;
101
    }
102
103
    /**
104
     * Find a channel by app & name or create one.
105
     *
106
     * @param  string|int  $appId
107
     * @param  string  $channel
108
     * @return BeyondCode\LaravelWebSockets\Channels\Channel
109
     */
110
    public function findOrCreate($appId, string $channel)
111
    {
112
        if (! $channelInstance = $this->find($appId, $channel)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $channelInstance is dead and can be removed.
Loading history...
113
            $class = $this->getChannelClassName($channel);
114
115
            $this->channels[$appId][$channel] = new $class($channel);
116
        }
117
118
        return $this->channels[$appId][$channel];
119
    }
120
121
    /**
122
     * Get the local connections, regardless of the channel
123
     * they are connected to.
124
     *
125
     * @return \React\Promise\PromiseInterface
126
     */
127
    public function getLocalConnections(): PromiseInterface
128
    {
129
        $connections = collect($this->channels)
130
            ->map(function ($channelsWithConnections, $appId) {
0 ignored issues
show
Unused Code introduced by
The parameter $appId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
            ->map(function ($channelsWithConnections, /** @scrutinizer ignore-unused */ $appId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
                return collect($channelsWithConnections)->values();
132
            })
133
            ->values()->collapse()
134
            ->map(function ($channel) {
135
                return collect($channel->getConnections());
136
            })
137
            ->values()->collapse()
138
            ->toArray();
139
140
        return Helpers::createFulfilledPromise($connections);
141
    }
142
143
    /**
144
     * Get all channels for a specific app
145
     * for the current instance.
146
     *
147
     * @param  string|int  $appId
148
     * @return \React\Promise\PromiseInterface[array]
0 ignored issues
show
Documentation Bug introduced by
The doc comment \React\Promise\PromiseInterface[array] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
149
     */
150
    public function getLocalChannels($appId): PromiseInterface
151
    {
152
        return Helpers::createFulfilledPromise(
153
            $this->channels[$appId] ?? []
154
        );
155
    }
156
157
    /**
158
     * Get all channel sockets for a specific app
159
     * for the current instance.
160
     *
161
     * @param  string|int  $appId
162
     * @return \React\Promise\PromiseInterface[array]
0 ignored issues
show
Documentation Bug introduced by
The doc comment \React\Promise\PromiseInterface[array] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
163
     */
164
    public function getChannelSockets($appId): PromiseInterface
165
    {
166
        return Helpers::createFulfilledPromise(
167
            collect($this->channelSockets[$appId] ?? [])
168
        );
169
    }
170
171
    /**
172
     * Get all channels for a specific app
173
     * across multiple servers.
174
     *
175
     * @param  string|int  $appId
176
     * @return \React\Promise\PromiseInterface[array]
0 ignored issues
show
Documentation Bug introduced by
The doc comment \React\Promise\PromiseInterface[array] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
177
     */
178
    public function getGlobalChannels($appId): PromiseInterface
179
    {
180
        return $this->getLocalChannels($appId);
181
    }
182
183
    /**
184
     * Remove connection from all channels.
185
     *
186
     * @param  \Ratchet\ConnectionInterface  $connection
187
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
188
     */
189
    public function unsubscribeFromAllChannels(ConnectionInterface $connection): PromiseInterface
190
    {
191
        if (! isset($connection->app)) {
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?
Loading history...
192
            return Helpers::createFulfilledPromise(false);
193
        }
194
195
        $this->getLocalChannels($connection->app->id)
196
            ->then(function ($channels) use ($connection) {
197
                collect($channels)
198
                    ->each(function (Channel $channel) use ($connection) {
199
                        $channel->unsubscribe($connection);
200
                    });
201
202
                collect($channels)
203
                    ->reject(function ($channel) {
204
                        return $channel->hasConnections();
205
                    })
206
                    ->each(function (Channel $channel, string $channelName) use ($connection) {
207
                        unset($this->channels[$connection->app->id][$channelName]);
208
                    });
209
            });
210
211
        $this->getLocalChannels($connection->app->id)
212
            ->then(function ($channels) use ($connection) {
213
                if (count($channels) === 0) {
214
                    unset($this->channels[$connection->app->id]);
215
                }
216
            });
217
218
        return Helpers::createFulfilledPromise(true);
219
    }
220
221
    /**
222
     * Subscribe the connection to a specific channel.
223
     *
224
     * @param  \Ratchet\ConnectionInterface  $connection
225
     * @param  string  $channelName
226
     * @param  stdClass  $payload
227
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
228
     */
229
    public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
230
    {
231
        $channel = $this->findOrCreate($connection->app->id, $channelName);
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?
Loading history...
232
233
        return Helpers::createFulfilledPromise(
234
            $channel->subscribe($connection, $payload)
235
        );
236
    }
237
238
    /**
239
     * Unsubscribe the connection from the channel.
240
     *
241
     * @param  \Ratchet\ConnectionInterface  $connection
242
     * @param  string  $channelName
243
     * @param  stdClass  $payload
244
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
245
     */
246
    public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
247
    {
248
        $channel = $this->findOrCreate($connection->app->id, $channelName);
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?
Loading history...
249
250
        return Helpers::createFulfilledPromise(
251
            $channel->unsubscribe($connection, $payload)
252
        );
253
    }
254
255
    /**
256
     * Subscribe the connection to a specific channel, returning
257
     * a promise containing the amount of connections.
258
     *
259
     * @param  string|int  $appId
260
     * @return PromiseInterface[int]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[int] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
261
     */
262
    public function subscribeToApp($appId): PromiseInterface
263
    {
264
        return Helpers::createFulfilledPromise(0);
265
    }
266
267
    /**
268
     * Unsubscribe the connection from the channel, returning
269
     * a promise containing the amount of connections after decrement.
270
     *
271
     * @param  string|int  $appId
272
     * @return PromiseInterface[int]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[int] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
273
     */
274
    public function unsubscribeFromApp($appId): PromiseInterface
275
    {
276
        return Helpers::createFulfilledPromise(0);
277
    }
278
279
    /**
280
     * Get the connections count on the app
281
     * for the current server instance.
282
     *
283
     * @param  string|int  $appId
284
     * @param  string|null  $channelName
285
     * @return PromiseInterface[int]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[int] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
286
     */
287
    public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface
288
    {
289
        return $this->getLocalChannels($appId)
290
            ->then(function ($channels) use ($channelName) {
291
                return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
292
                    return $collection->filter(function (Channel $channel) use ($channelName) {
293
                        return $channel->getName() === $channelName;
294
                    });
295
                })
296
                    ->flatMap(function (Channel $channel) {
297
                        return collect($channel->getConnections())->pluck('socketId');
298
                    })
299
                    ->unique()->count();
300
            });
301
    }
302
303
    /**
304
     * Get the connections count
305
     * across multiple servers.
306
     *
307
     * @param  string|int  $appId
308
     * @param  string|null  $channelName
309
     * @return PromiseInterface[int]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[int] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
310
     */
311
    public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface
312
    {
313
        return $this->getLocalConnectionsCount($appId, $channelName);
314
    }
315
316
    /**
317
     * Broadcast the message across multiple servers.
318
     *
319
     * @param  string|int  $appId
320
     * @param  string|null  $socketId
321
     * @param  string  $channel
322
     * @param  stdClass  $payload
323
     * @param  string|null  $serverId
324
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
325
     */
326
    public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null): PromiseInterface
327
    {
328
        return Helpers::createFulfilledPromise(true);
329
    }
330
331
    /**
332
     * Handle joining a channel.
333
     *
334
     * @param  \Ratchet\ConnectionInterface  $connection
335
     * @param  stdClass  $user
336
     * @param  string  $channel
337
     * @return void
338
     */
339
    public function joinedChannel(ConnectionInterface $connection, string $channel): void
340
    {
341
        $this->channelSockets[$connection->app->id][$channel][$connection->socketId] = 1;
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?
Loading history...
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
342
    }
343
344
    /**
345
     * Handle leaving a channel.
346
     *
347
     * @param  \Ratchet\ConnectionInterface  $connection
348
     * @param  string  $channel
349
     * @return void
350
     */
351
    public function leftChannel(ConnectionInterface $connection, string $channel): void
352
    {
353
        unset($this->channelSockets[$connection->app->id][$channel][$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?
Loading history...
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
354
355
        // cleanup channels
356
        if (count($this->channelSockets[$connection->app->id][$channel]) < 1) {
357
            unset($this->channelSockets[$connection->app->id][$channel]);
358
        }
359
    }
360
361
    /**
362
     * Handle the user when it joined a presence channel.
363
     *
364
     * @param  \Ratchet\ConnectionInterface  $connection
365
     * @param  stdClass  $user
366
     * @param  string  $channel
367
     * @param  stdClass  $payload
368
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
369
     */
370
    public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload): PromiseInterface
371
    {
372
        $this->users["{$connection->app->id}:{$channel}"][$connection->socketId] = json_encode($user);
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?
Loading history...
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
373
        $this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"][] = $connection->socketId;
374
375
        return Helpers::createFulfilledPromise(true);
376
    }
377
378
    /**
379
     * Handle the user when it left a presence channel.
380
     *
381
     * @param  \Ratchet\ConnectionInterface  $connection
382
     * @param  stdClass  $user
383
     * @param  string  $channel
384
     * @param  stdClass  $payload
385
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
386
     */
387
    public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel): PromiseInterface
388
    {
389
        unset($this->users["{$connection->app->id}:{$channel}"][$connection->socketId]);
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?
Loading history...
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
390
391
        $deletableSocketKey = array_search(
392
            $connection->socketId,
393
            $this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]
394
        );
395
396
        if ($deletableSocketKey !== false) {
397
            unset($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"][$deletableSocketKey]);
398
399
            if (count($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]) === 0) {
400
                unset($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]);
401
            }
402
        }
403
404
        return Helpers::createFulfilledPromise(true);
405
    }
406
407
    /**
408
     * Get the presence channel members.
409
     *
410
     * @param  string|int  $appId
411
     * @param  string  $channel
412
     * @return \React\Promise\PromiseInterface
413
     */
414
    public function getChannelMembers($appId, string $channel): PromiseInterface
415
    {
416
        $members = $this->users["{$appId}:{$channel}"] ?? [];
417
418
        $members = collect($members)->map(function ($user) {
419
            return json_decode($user);
420
        })->unique('user_id')->toArray();
421
422
        return Helpers::createFulfilledPromise($members);
423
    }
424
425
    /**
426
     * Get a member from a presence channel based on connection.
427
     *
428
     * @param  \Ratchet\ConnectionInterface  $connection
429
     * @param  string  $channel
430
     * @return \React\Promise\PromiseInterface
431
     */
432
    public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface
433
    {
434
        $member = $this->users["{$connection->app->id}:{$channel}"][$connection->socketId] ?? null;
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?
Loading history...
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
435
436
        return Helpers::createFulfilledPromise($member);
437
    }
438
439
    /**
440
     * Get the channels total sockets count.
441
     *
442
     * @param  string|int  $appId
443
     * @param  array  $channelNames
444
     * @return \React\Promise\PromiseInterface
445
     */
446
    public function getChannelsSocketsCount($appId, array $channelNames): PromiseInterface
447
    {
448
        $results = collect($channelNames)
449
            ->reduce(function ($results, $channel) use ($appId) {
450
                $results[$channel] = isset($this->channelSockets[$appId][$channel])
451
                    ? count($this->channelSockets[$appId][$channel])
452
                    : 0;
453
454
                return $results;
455
            }, []);
456
457
        return Helpers::createFulfilledPromise($results);
458
    }
459
460
    /**
461
     * Get the presence channels total members count.
462
     *
463
     * @param  string|int  $appId
464
     * @param  array  $channelNames
465
     * @return \React\Promise\PromiseInterface
466
     */
467
    public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface
468
    {
469
        $results = collect($channelNames)
470
            ->reduce(function ($results, $channel) use ($appId) {
471
                $results[$channel] = isset($this->users["{$appId}:{$channel}"])
472
                    ? count($this->users["{$appId}:{$channel}"])
473
                    : 0;
474
475
                return $results;
476
            }, []);
477
478
        return Helpers::createFulfilledPromise($results);
479
    }
480
481
    /**
482
     * Get the socket IDs for a presence channel member.
483
     *
484
     * @param  string|int  $userId
485
     * @param  string|int  $appId
486
     * @param  string  $channelName
487
     * @return \React\Promise\PromiseInterface
488
     */
489
    public function getMemberSockets($userId, $appId, $channelName): PromiseInterface
490
    {
491
        return Helpers::createFulfilledPromise(
492
            $this->userSockets["{$appId}:{$channelName}:{$userId}"] ?? []
493
        );
494
    }
495
496
    /**
497
     * Keep tracking the connections availability when they pong.
498
     *
499
     * @param  \Ratchet\ConnectionInterface  $connection
500
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
501
     */
502
    public function connectionPonged(ConnectionInterface $connection): PromiseInterface
503
    {
504
        return $this->pongConnectionInChannels($connection);
505
    }
506
507
    /**
508
     * Remove the obsolete connections that didn't ponged in a while.
509
     *
510
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
511
     */
512
    public function removeObsoleteConnections(): PromiseInterface
513
    {
514
        $lock = $this->lock();
515
        try {
516
            if (! $lock->acquire()) {
517
                return Helpers::createFulfilledPromise(false);
518
            }
519
520
            $this->getLocalConnections()->then(function ($connections) {
521
                foreach ($connections as $connection) {
522
                    $differenceInSeconds = $connection->lastPongedAt->diffInSeconds(Carbon::now());
523
524
                    if ($differenceInSeconds > 120) {
525
                        $this->unsubscribeFromAllChannels($connection);
526
                    }
527
                }
528
            });
529
530
            return Helpers::createFulfilledPromise(true);
531
        } finally {
532
            optional($lock)->forceRelease();
533
        }
534
    }
535
536
    /**
537
     * Pong connection in channels.
538
     *
539
     * @param  ConnectionInterface  $connection
540
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
541
     */
542
    public function pongConnectionInChannels(ConnectionInterface $connection): PromiseInterface
543
    {
544
        return $this->getLocalChannels($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?
Loading history...
545
            ->then(function ($channels) use ($connection) {
546
                foreach ($channels as $channel) {
547
                    if ($conn = $channel->getConnection($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?
Loading history...
548
                        $conn->lastPongedAt = Carbon::now();
549
                        $channel->saveConnection($conn);
550
                    }
551
                }
552
553
                return true;
554
            });
555
    }
556
557
    /**
558
     * Update the connection in all channels.
559
     *
560
     * @param  ConnectionInterface  $connection
561
     * @return PromiseInterface[bool]
0 ignored issues
show
Documentation Bug introduced by
The doc comment PromiseInterface[bool] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
562
     */
563
    public function updateConnectionInChannels($connection): PromiseInterface
564
    {
565
        return $this->getLocalChannels($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?
Loading history...
566
            ->then(function ($channels) use ($connection) {
567
                foreach ($channels as $channel) {
568
                    if ($channel->hasConnection($connection)) {
569
                        $channel->saveConnection($connection);
570
                    }
571
                }
572
573
                return true;
574
            });
575
    }
576
577
    /**
578
     * Mark the current instance as unable to accept new connections.
579
     *
580
     * @return $this
581
     */
582
    public function declineNewConnections()
583
    {
584
        $this->acceptsNewConnections = false;
585
586
        return $this;
587
    }
588
589
    /**
590
     * Check if the current server instance
591
     * accepts new connections.
592
     *
593
     * @return bool
594
     */
595
    public function acceptsNewConnections(): bool
596
    {
597
        return $this->acceptsNewConnections;
598
    }
599
600
    /**
601
     * Get the channel class by the channel name.
602
     *
603
     * @param  string  $channelName
604
     * @return string
605
     */
606
    protected function getChannelClassName(string $channelName): string
607
    {
608
        if (Str::startsWith($channelName, 'private-')) {
609
            return PrivateChannel::class;
610
        }
611
612
        if (Str::startsWith($channelName, 'presence-')) {
613
            return PresenceChannel::class;
614
        }
615
616
        return Channel::class;
617
    }
618
619
    /**
620
     * Get the unique identifier for the server.
621
     *
622
     * @return string
623
     */
624
    public function getServerId(): string
625
    {
626
        return $this->serverId;
627
    }
628
629
    /**
630
     * Get a new ArrayLock instance to avoid race conditions.
631
     *
632
     * @return \Illuminate\Cache\CacheLock
633
     */
634
    protected function lock()
635
    {
636
        return new ArrayLock($this->store, static::$lockName, 0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Illuminate\Ca...e, static::lockName, 0) returns the type Illuminate\Cache\ArrayLock which is incompatible with the documented return type Illuminate\Cache\CacheLock.
Loading history...
637
    }
638
}
639