Completed
Push — master ( edcf20...3ec6c0 )
by Marcel
03:05 queued 01:22
created

Channels/ChannelManagers/ArrayChannelManager.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Ratchet\ConnectionInterface;
8
use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel;
9
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
10
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PrivateChannel;
11
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
12
13
class ArrayChannelManager implements ChannelManager
14
{
15
    /** @var string */
16
    protected $appId;
17
18
    /** @var array */
19
    protected $channels = [];
20
21
    public function findOrCreate(string $appId, string $channelName): Channel
22
    {
23
        if (! isset($this->channels[$appId][$channelName])) {
24
            $channelClass = $this->determineChannelClass($channelName);
25
26
            $this->channels[$appId][$channelName] = new $channelClass($channelName);
27
        }
28
29
        return $this->channels[$appId][$channelName];
30
    }
31
32
    public function find(string $appId, string $channelName): ?Channel
33
    {
34
        return $this->channels[$appId][$channelName] ?? null;
35
    }
36
37
    protected function determineChannelClass(string $channelName): string
38
    {
39
        if (Str::startsWith($channelName, 'private-')) {
40
            return PrivateChannel::class;
41
        }
42
43
        if (Str::startsWith($channelName, 'presence-')) {
44
            return PresenceChannel::class;
45
        }
46
47
        return Channel::class;
48
    }
49
50
    public function getChannels(string $appId): array
51
    {
52
        return $this->channels[$appId] ?? [];
53
    }
54
55
    public function getConnectionCount(string $appId): int
56
    {
57
        return collect($this->getChannels($appId))
58
            ->sum(function ($channel) {
59
                return count($channel->getSubscribedConnections());
60
            });
61
    }
62
63
    public function removeFromAllChannels(ConnectionInterface $connection)
64
    {
65
        if (! isset($connection->app)) {
0 ignored issues
show
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...
66
            return;
67
        }
68
69
        /*
70
         * Remove the connection from all channels.
71
         */
72
        collect(Arr::get($this->channels, $connection->app->id, []))->each->unsubscribe($connection);
0 ignored issues
show
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...
73
74
        /*
75
         * Unset all channels that have no connections so we don't leak memory.
76
         */
77
        collect(Arr::get($this->channels, $connection->app->id, []))
0 ignored issues
show
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...
78
            ->reject->hasConnections()
79
                    ->each(function (Channel $channel, string $channelName) use ($connection) {
80
                        unset($this->channels[$connection->app->id][$channelName]);
81
                    });
82
83
        if (count(Arr::get($this->channels, $connection->app->id, [])) === 0) {
0 ignored issues
show
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...
84
            unset($this->channels[$connection->app->id]);
85
        }
86
    }
87
}
88