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:14
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
            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...
61
62
        } else {
63
            throw new HttpException(401);
64
        }
65
    }
66
67
    /**
68
     * Return the valid authentication response.
69
     *
70
     * @param \Illuminate\Http\Request $request
71
     * @param mixed $result
72
     * @return mixed
73
     */
74
    public function validAuthenticationResponse($request, $result)
75
    {
76
        return $result;
77
    }
78
79
    /**
80
     * Broadcast the given event.
81
     *
82
     * @param array $channels
83
     * @param string $event
84
     * @param array $payload
85
     * @return void
86
     */
87
    public function broadcast(array $channels, $event, array $payload = [])
88
    {
89
        $payload['event'] = $event;
90
        $channels = array_map(function ($channel){
91
            $channel = str_replace('private-', '$', $channel);
0 ignored issues
show
Unused Code introduced by
$channel is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        });
93
94
        $response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload);
95
96
        if (is_array($response) && !isset($response['error'])) {
97
            return;
98
        }
99
100
        throw new BroadcastException(
101
            $response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error']
102
        );
103
    }
104
105
    /**
106
     * Get client from request.
107
     *
108
     * @param \Illuminate\Http\Request $request
109
     * @return string
110
     */
111
    private function getClientFromRequest($request)
112
    {
113
        return $request->get('client', '');
114
    }
115
116
    /**
117
     * Get channels from request.
118
     *
119
     * @param \Illuminate\Http\Request $request
120
     * @return array
121
     */
122
    private function getChannelsFromRequest($request)
123
    {
124
        $channels = $request->get('channels', []);
125
126
        return is_array($channels) ? $channels : [$channels];
127
    }
128
129
    /**
130
     * Get channel name without $ symbol (if present).
131
     *
132
     * @param string $channel
133
     * @return string
134
     */
135
    private function getChannelName(string $channel)
136
    {
137
        return $this->isPrivateChannel($channel) ? substr($channel, 1) : $channel;
138
    }
139
140
    /**
141
     * Check channel name by $ symbol
142
     * @param string $channel
143
     * @return bool
144
     */
145
    private function isPrivateChannel(string $channel): bool
146
    {
147
        return substr($channel, 0, 1) === '$';
148
    }
149
150
    /**
151
     * Make response for client, based on access rights.
152
     *
153
     * @param bool $access_granted
154
     * @param string $client
155
     * @return array
156
     */
157
    private function makeResponseForClient(bool $access_granted, string $client)
158
    {
159
        $info = [];
160
161
        return $access_granted ? [
162
            'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info),
163
            'info' => $info,
164
        ] : [
165
            'status' => 403,
166
        ];
167
    }
168
169
    /**
170
     * Make response for client, based on access rights of private channel.
171
     *
172
     * @param bool $access_granted
173
     * @param string $channel
174
     * @param string $client
175
     * @return array
176
     */
177
    private function makeResponseForPrivateClient(bool $access_granted, string $channel, string $client)
178
    {
179
        $info = [];
180
181
        return $access_granted ? [
182
183
            'channel' => $channel,
184
            'token' => $this->centrifugo->generatePrivateChannelToken($client, $channel, 0, $info),
185
            'info' => $this->centrifugo->info()
186
187
        ] : [
188
            'status' => 403,
189
        ];
190
    }
191
}
192