Completed
Push — master ( b70f2b...5fe2c3 )
by Nikita
02:13
created

CentrifugeBroadcaster::validAuthenticationResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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) {
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;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_RETURN
Loading history...
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