TriggerEvent   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 47 6
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\API;
4
5
use BeyondCode\LaravelWebSockets\DashboardLogger;
6
use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector;
7
use Illuminate\Http\Request;
8
9
class TriggerEvent extends Controller
10
{
11
    /**
12
     * Handle the incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function __invoke(Request $request)
18
    {
19
        $channels = $request->channels ?: [];
20
21
        if (is_string($channels)) {
22
            $channels = [$channels];
23
        }
24
25
        foreach ($channels as $channelName) {
26
            // Here you can use the ->find(), even if the channel
27
            // does not exist on the server. If it does not exist,
28
            // then the message simply will get broadcasted
29
            // across the other servers.
30
            $channel = $this->channelManager->find(
31
                $request->appId, $channelName
32
            );
33
34
            $payload = [
35
                'event' => $request->name,
36
                'channel' => $channelName,
37
                'data' => $request->data,
38
            ];
39
40
            if ($channel) {
41
                $channel->broadcastLocallyToEveryoneExcept(
42
                    (object) $payload,
43
                    $request->socket_id,
44
                    $request->appId
45
                );
46
            }
47
48
            $this->channelManager->broadcastAcrossServers(
49
                $request->appId, $request->socket_id, $channelName, (object) $payload
50
            );
51
52
            if ($this->app->statisticsEnabled) {
53
                StatisticsCollector::apiMessage($request->appId);
54
            }
55
56
            DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
57
                'event' => $request->name,
58
                'channel' => $channelName,
59
                'payload' => $request->data,
60
            ]);
61
        }
62
63
        return (object) [];
64
    }
65
}
66