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