Completed
Push — master ( 1227bc...65ae99 )
by Alexandru
01:22
created

SendMessage::getPusherBroadcaster()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
4
5
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
6
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
7
use Illuminate\Http\Request;
8
use Pusher\Pusher;
9
10
class SendMessage
11
{
12
    public function __invoke(Request $request)
13
    {
14
        $validated = $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
15
            'appId' => ['required', new AppId()],
16
            'key' => 'required',
17
            'secret' => 'required',
18
            'channel' => 'required',
19
            'event' => 'required',
20
            'data' => 'json',
21
        ]);
22
23
        $this->getPusherBroadcaster($validated)->broadcast(
24
            [$validated['channel']],
25
            $validated['event'],
26
            json_decode($validated['data'], true)
27
        );
28
29
        return 'ok';
30
    }
31
32
    protected function getPusherBroadcaster(array $validated): PusherBroadcaster
33
    {
34
        $pusher = new Pusher(
35
            $validated['key'],
36
            $validated['secret'],
37
            $validated['appId'],
38
            config('broadcasting.connections.pusher.options', [])
39
        );
40
41
        return new PusherBroadcaster($pusher);
42
    }
43
}
44