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:06
created

makeResponseForPrivateClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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