Completed
Pull Request — master (#447)
by Marcel
03:00 queued 01:29
created

FetchChannels   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 8
dl 0
loc 69
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 60 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\API;
4
5
use BeyondCode\LaravelWebSockets\Channels\Channel;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
use stdClass;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
class FetchChannels extends Controller
12
{
13
    /**
14
     * Handle the incoming request.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function __invoke(Request $request)
20
    {
21
        $attributes = [];
22
23
        if ($request->has('info')) {
24
            $attributes = explode(',', trim($request->info));
25
26
            if (in_array('user_count', $attributes) && ! Str::startsWith($request->filter_by_prefix, 'presence-')) {
27
                throw new HttpException(400, 'Request must be limited to presence channels in order to fetch user_count');
28
            }
29
        }
30
31
        return $this->channelManager
32
            ->getGlobalChannels($request->appId)
33
            ->then(function ($channels) use ($request, $attributes) {
34
                $channels = collect($channels)->keyBy(function ($channel) {
35
                    return $channel instanceof Channel
36
                        ? $channel->getName()
37
                        : $channel;
38
                });
39
40
                if ($request->has('filter_by_prefix')) {
41
                    $channels = $channels->filter(function ($channel, $channelName) use ($request) {
42
                        return Str::startsWith($channelName, $request->filter_by_prefix);
43
                    });
44
                }
45
46
                $channelNames = $channels->map(function ($channel) {
47
                    return $channel instanceof Channel
48
                        ? $channel->getName()
49
                        : $channel;
50
                })->toArray();
51
52
                return $this->channelManager
53
                    ->getChannelsMembersCount($request->appId, $channelNames)
54
                    ->then(function ($counts) use ($channels, $attributes) {
55
                        $channels = $channels->map(function ($channel) use ($counts, $attributes) {
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $channels, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
56
                            $info = new stdClass;
57
58
                            $channelName = $channel instanceof Channel
59
                                ? $channel->getName()
60
                                : $channel;
61
62
                            if (in_array('user_count', $attributes)) {
63
                                $info->user_count = $counts[$channelName];
64
                            }
65
66
                            return $info;
67
                        })
68
                        ->sortBy(function ($content, $name) {
69
                            return $name;
70
                        })
71
                        ->all();
72
73
                        return [
74
                            'channels' => $channels ?: new stdClass,
75
                        ];
76
                    });
77
            });
78
    }
79
}
80