Completed
Push — master ( 821bc6...f04b8e )
by Alex
02:06
created

LaracentBroadcaster::getChannelName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace AlexHnydiuk\Laracent;
4
5
use Exception;
6
use Illuminate\Broadcasting\BroadcastException;
7
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
use AlexHnydiuk\Laracent\Contracts\Centrifugo as Centrifugo;
10
11
class LaracentBroadcaster extends Broadcaster
12
{
13
    /**
14
     * The Centrifugo SDK instance.
15
     *
16
     * @var \AlexHnydiuk\Laracent\Contracts\Centrifugo
17
     */
18
    protected $centrifugo;
19
20
    /**
21
     * Create a new broadcaster instance.
22
     *
23
     * @param  \AlexHnydiuk\Laracent\Contracts\Centrifugo  $centrifugo
24
     */
25
    public function __construct(Centrifugo $centrifugo)
26
    {
27
        $this->centrifugo = $centrifugo;
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 = $this->getClientFromRequest($request);
0 ignored issues
show
Unused Code introduced by
The assignment to $client is dead and can be removed.
Loading history...
40
            $channels = $this->getChannelsFromRequest($request);
41
42
            $response = [];
43
            foreach ($channels as $channel) {
44
                $channelName = $this->getChannelName($channel);
45
46
                try {
47
                    $is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName);
48
                } catch (HttpException $e) {
49
                    $is_access_granted = false;
50
                }
51
52
                $response[$channel] = $this->makeResponseForClient($is_access_granted);
0 ignored issues
show
Bug introduced by
The call to AlexHnydiuk\Laracent\Lar...makeResponseForClient() has too few arguments starting with client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                /** @scrutinizer ignore-call */ 
53
                $response[$channel] = $this->makeResponseForClient($is_access_granted);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
            }
54
55
            return response()->json($response);
56
        } else {
57
            throw new HttpException(401);
58
        }
59
    }
60
61
    /**
62
     * Return the valid authentication response.
63
     *
64
     * @param  \Illuminate\Http\Request  $request
65
     * @param  mixed  $result
66
     * @return mixed
67
     */
68
    public function validAuthenticationResponse($request, $result)
69
    {
70
        return $result;
71
    }
72
73
    /**
74
     * Broadcast the given event.
75
     *
76
     * @param  array  $channels
77
     * @param  string  $event
78
     * @param  array  $payload
79
     * @return void
80
     */
81
    public function broadcast(array $channels, $event, array $payload = [])
82
    {
83
        $payload['event'] = $event;
84
85
        $response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload);
86
87
        if (is_array($response) && ! isset($response['error'])) {
88
            return;
89
        }
90
91
        throw new BroadcastException(
92
            $response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error']
93
        );
94
    }
95
96
    /**
97
     * Get client from request
98
     *
99
     * @param  \Illuminate\Http\Request  $request
100
     * @return string
101
     */
102
    private function getClientFromRequest($request)
103
    {
104
        return $request->get('client', '');
105
    }
106
107
    /**
108
     * Get channels from request
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @return array
112
     */
113
    private function getChannelsFromRequest($request)
114
    {
115
        $channels = $request->get('channels', []);
116
117
        return is_array($channels) ? $channels : [$channels];
118
    }
119
120
    /**
121
     * Get channel name without $ symbol (if present)
122
     *
123
     * @param  string  $channel
124
     * @return string
125
     */
126
    private function getChannelName(string $channel)
127
    {
128
        return (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel;
129
    }
130
131
    /**
132
     * Make response for client, based on access rights
133
     *
134
     * @param  bool  $access_granted
135
     * @param  string $client
136
     * @return array
137
     */
138
    private function makeResponseForClient(bool $access_granted, string $client)
139
    {
140
        $info = [];
141
142
        return $access_granted ? [
143
            'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info),
144
            'info' => $info,
145
        ] : [
146
            'status' => 403,
147
        ];
148
    }
149
}
150