Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#3)
by
unknown
01:08
created

CentrifugoBroadcaster   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 0
loc 183
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B auth() 0 31 6
A validAuthenticationResponse() 0 4 1
A broadcast() 0 17 4
A getClientFromRequest() 0 4 1
A getChannelsFromRequest() 0 6 2
A getChannelName() 0 4 2
A isPrivateChannel() 0 4 1
A makeResponseForClient() 0 11 2
A makeResponseForPrivateClient() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace denis660\Centrifugo;
6
7
use Exception;
8
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
9
use Illuminate\Broadcasting\BroadcastException;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
12
class CentrifugoBroadcaster extends Broadcaster
13
{
14
    /**
15
     * The Centrifugo SDK instance.
16
     *
17
     * @var Contracts\CentrifugoInterface
18
     */
19
    protected $centrifugo;
20
21
    /**
22
     * Create a new broadcaster instance.
23
     *
24
     * @param Centrifugo $centrifugo
25
     */
26
    public function __construct(Centrifugo $centrifugo)
27
    {
28
        $this->centrifugo = $centrifugo;
29
    }
30
31
    /**
32
     * Authenticate the incoming request for a given channel.
33
     *
34
     * @param \Illuminate\Http\Request $request
35
     * @return mixed
36
     */
37
    public function auth($request)
38
    {
39
        if ($request->user()) {
40
            $client = $this->getClientFromRequest($request);
41
            $channels = $this->getChannelsFromRequest($request);
42
43
            $response = [];
44
            $privateResponse = [];
45
            foreach ($channels as $channel) {
46
                $channelName = $this->getChannelName($channel);
47
48
                try {
49
                    $is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName);
50
                } catch (HttpException $e) {
51
                    $is_access_granted = false;
52
                }
53
54
                if ($private = $this->isPrivateChannel($channel)) {
55
                    $privateResponse['channels'][] = $this->makeResponseForPrivateClient($is_access_granted, $channel, $client);
56
                }
57
                else {
58
                    $response[$channel] = $this->makeResponseForClient($is_access_granted, $client);
59
                }
60
            }
61
62
            return response($private ? $privateResponse : $response);
0 ignored issues
show
Bug introduced by
The variable $private does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
63
64
        } else {
65
            throw new HttpException(401);
66
        }
67
    }
68
69
    /**
70
     * Return the valid authentication response.
71
     *
72
     * @param \Illuminate\Http\Request $request
73
     * @param mixed $result
74
     * @return mixed
75
     */
76
    public function validAuthenticationResponse($request, $result)
77
    {
78
        return $result;
79
    }
80
81
    /**
82
     * Broadcast the given event.
83
     *
84
     * @param array $channels
85
     * @param string $event
86
     * @param array $payload
87
     * @return void
88
     */
89
    public function broadcast(array $channels, $event, array $payload = [])
90
    {
91
        $payload['event'] = $event;
92
        $channels = array_map(function ($channel) {
93
            return str_replace('private-', '$', $channel);
94
        }, $channels);
95
96
        $response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload);
97
98
        if (is_array($response) && !isset($response['error'])) {
99
            return;
100
        }
101
102
        throw new BroadcastException(
103
            $response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error']
104
        );
105
    }
106
107
    /**
108
     * Get client from request.
109
     *
110
     * @param \Illuminate\Http\Request $request
111
     * @return string
112
     */
113
    private function getClientFromRequest($request)
114
    {
115
        return $request->get('client', '');
116
    }
117
118
    /**
119
     * Get channels from request.
120
     *
121
     * @param \Illuminate\Http\Request $request
122
     * @return array
123
     */
124
    private function getChannelsFromRequest($request)
125
    {
126
        $channels = $request->get('channels', []);
127
128
        return is_array($channels) ? $channels : [$channels];
129
    }
130
131
    /**
132
     * Get channel name without $ symbol (if present).
133
     *
134
     * @param string $channel
135
     * @return string
136
     */
137
    private function getChannelName(string $channel)
138
    {
139
        return $this->isPrivateChannel($channel) ? substr($channel, 1) : $channel;
140
    }
141
142
    /**
143
     * Check channel name by $ symbol
144
     *
145
     * @param string $channel
146
     * @return bool
147
     */
148
    private function isPrivateChannel(string $channel): bool
149
    {
150
        return substr($channel, 0, 1) === '$';
151
    }
152
153
    /**
154
     * Make response for client, based on access rights.
155
     *
156
     * @param bool $access_granted
157
     * @param string $client
158
     * @return array
159
     */
160
    private function makeResponseForClient(bool $access_granted, string $client)
161
    {
162
        $info = [];
163
164
        return $access_granted ? [
165
            'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info),
166
            'info' => $info
167
        ] : [
168
            'status' => 403,
169
        ];
170
    }
171
172
    /**
173
     * Make response for client, based on access rights of private channel.
174
     *
175
     * @param bool $access_granted
176
     * @param string $channel
177
     * @param string $client
178
     * @return array
179
     */
180
    private function makeResponseForPrivateClient(bool $access_granted, string $channel, string $client)
181
    {
182
        $info = [];
183
184
        return $access_granted ? [
185
186
            'channel' => $channel,
187
            'token' => $this->centrifugo->generatePrivateChannelToken($client, $channel, 0, $info),
188
            'info' => $this->centrifugo->info()
189
190
        ] : [
191
            'status' => 403,
192
        ];
193
    }
194
}
195