Completed
Pull Request — master (#7)
by Antonio
04:05
created

SendMessage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 19 1
A getPusherBroadcaster() 0 11 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
4
5
use Pusher\Pusher;
6
use Illuminate\Http\Request;
7
use Illuminate\Contracts\Config\Repository;
8
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
9
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
10
11
class SendMessage
12
{
13
    /** @var \Illuminate\Contracts\Config\Repository */
14
    protected $config;
15
16
    public function __construct(Repository $config)
17
    {
18
        $this->config = $config;
19
    }
20
21
    public function __invoke(Request $request)
22
    {
23
        $validated = $request->validate([
24
            'appId' => ['required', new AppId()],
25
            'key' => 'required',
26
            'secret' => 'required',
27
            'channel' => 'required',
28
            'event' => 'required',
29
            'data' => 'json',
30
        ]);
31
32
        $this->getPusherBroadcaster($validated)->broadcast(
33
            [$validated['channel']],
34
            $validated['event'],
35
            json_decode($validated['data'], true)
36
        );
37
38
        return 'ok';
39
    }
40
41
    protected function getPusherBroadcaster(array $validated): PusherBroadcaster
42
    {
43
        $pusher = new Pusher(
44
            $validated['key'],
45
            $validated['secret'],
46
            $validated['appId'],
47
            $this->config->get('broadcasting.connections.pusher.options', [])
48
        );
49
50
        return new PusherBroadcaster($pusher);
51
    }
52
}
53