Completed
Pull Request — master (#41)
by
unknown
01:30
created

ArrayChannelManager::getConnectionCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers;
4
5
use Ratchet\ConnectionInterface;
6
use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel;
7
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
8
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PrivateChannel;
9
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
10
11
12
class ArrayChannelManager implements ChannelManager
13
{
14
    /** @var string */
15
    protected $appId;
16
17
    /** @var array */
18
    protected $channels = [];
19
20
    public function findOrCreate(string $appId, string $channelName): Channel
21
    {
22
        if (! isset($this->channels[$appId][$channelName])) {
23
            $channelClass = $this->determineChannelClass($channelName);
24
25
            $this->channels[$appId][$channelName] = new $channelClass($channelName);
26
        }
27
28
        return $this->channels[$appId][$channelName];
29
    }
30
31
    public function find(string $appId, string $channelName): ?Channel
32
    {
33
        return $this->channels[$appId][$channelName] ?? null;
34
    }
35
36
    protected function determineChannelClass(string $channelName): string
37
    {
38
        if (starts_with($channelName, 'private-')) {
39
            return PrivateChannel::class;
40
        }
41
42
        if (starts_with($channelName, 'presence-')) {
43
            return PresenceChannel::class;
44
        }
45
46
        return Channel::class;
47
    }
48
49
    public function getChannels(string $appId): array
50
    {
51
        return $this->channels[$appId] ?? [];
52
    }
53
54
    public function getConnectionCount(string $appId): int
55
    {
56
        return collect($this->getChannels($appId))
57
            ->sum(function ($channel) {
58
                return count($channel->getSubscribedConnections());
59
            });
60
    }
61
62
    public function removeFromAllChannels(ConnectionInterface $connection)
63
    {
64
        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?

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...
65
            return;
66
        }
67
68
        /*
69
         * Remove the connection from all channels.
70
         */
71
        collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection);
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...
72
73
        /*
74
         * Unset all channels that have no connections so we don't leak memory.
75
         */
76
        collect(array_get($this->channels, $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...
77
            ->reject->hasConnections()
78
                    ->each(function (Channel $channel, string $channelName) use ($connection) {
79
                        unset($this->channels[$connection->app->id][$channelName]);
80
                    });
81
82
        if (count(array_get($this->channels, $connection->app->id, [])) === 0) {
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...
83
            unset($this->channels[$connection->app->id]);
84
        }
85
    }
86
}
87