Completed
Push — master ( 5fe2c3...6d7547 )
by Nikita
01:35
created

CentrifugeBroadcaster::broadcast()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace LaraComponents\Centrifuge;
4
5
use Exception;
6
use Illuminate\Broadcasting\BroadcastException;
7
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
use LaraComponents\Centrifuge\Contracts\Centrifuge as CentrifugeContract;
10
11
class CentrifugeBroadcaster extends Broadcaster
12
{
13
    /**
14
     * The Centrifuge SDK instance.
15
     *
16
     * @var \LaraComponents\Centrifuge\Contracts\Centrifuge
17
     */
18
    protected $centrifuge;
19
20
    /**
21
     * Create a new broadcaster instance.
22
     *
23
     * @param  \LaraComponents\Centrifuge\Contracts\Centrifuge  $centrifuge
24
     */
25
    public function __construct(CentrifugeContract $centrifuge)
26
    {
27
        $this->centrifuge = $centrifuge;
28
    }
29
30
    /**
31
     * Authenticate the incoming request for a given channel.
32
     *
33
     * @param  \Illuminate\Http\Request  $request
34
     * @return mixed
35
     */
36
    public function auth($request)
37
    {
38
        if ($request->user()) {
39
            $client = $request->get('client', '');
40
            $channels = $request->get('channels', []);
41
            $channels = is_array($channels) ? $channels : [$channels];
42
43
            $response = [];
44
            $info = json_encode([]);
45
            foreach ($channels as $channel) {
46
                try {
47
                    $result = $this->verifyUserCanAccessChannel($request, $channel);
48
                } catch (HttpException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpKe...Exception\HttpException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
49
                    $result = false;
50
                }
51
52
                $response[$channel] = $result ? [
53
                    'sign' => $this->centrifuge->generateToken($client, $channel, $info),
54
                    'info' => $info,
55
                ] : [
56
                    'status' => 403,
57
                ];
58
            }
59
60
            return response()->json($response);
61
        } else {
62
            throw new HttpException(401);
63
        }
64
    }
65
66
    /**
67
     * Return the valid authentication response.
68
     *
69
     * @param  \Illuminate\Http\Request  $request
70
     * @param  mixed  $result
71
     * @return mixed
72
     */
73
    public function validAuthenticationResponse($request, $result)
74
    {
75
        return $result;
76
    }
77
78
    /**
79
     * Broadcast the given event.
80
     *
81
     * @param  array  $channels
82
     * @param  string  $event
83
     * @param  array  $payload
84
     * @return void
85
     */
86
    public function broadcast(array $channels, $event, array $payload = [])
87
    {
88
        $payload['event'] = $event;
89
90
        $response = $this->centrifuge->broadcast($this->formatChannels($channels), $payload);
91
92
        if (is_array($response) && is_null($response['error'])) {
93
            return;
94
        }
95
96
        throw new BroadcastException($response['error']);
97
    }
98
99
    /**
100
     * Get the Centrifuge instance.
101
     *
102
     * @return \LaraComponents\Centrifuge\Contracts\Centrifuge
103
     */
104
    public function getCentrifuge()
105
    {
106
        return $this->centrifuge;
107
    }
108
}
109