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
Push — master ( 24da77...3cb3b4 )
by Denis
01:19 queued 11s
created

CentrifugoBroadcaster::getChannelName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace denis660\Centrifugo;
5
6
use denis660\Centrifugo\Contracts\CentrifugoInterface;
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 \denis660\Centrifugo\Contracts\CentrifugoInterface
18
     */
19
    protected $centrifugo;
20
21
    /**
22
     * Create a new broadcaster instance.
23
     *
24
     * @param \denis660\Centrifugo\Contracts\CentrifugoInterface $centrifugo
25
     */
26
    public function __construct(CentrifugoInterface $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
            foreach ($channels as $channel) {
45
                $channelName = $this->getChannelName($channel);
46
47
                try {
48
                    $is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName);
49
                } catch (HttpException $e) {
50
                    $is_access_granted = false;
51
                }
52
53
                $response[$channel] = $this->makeResponseForClient($is_access_granted, $client);
54
            }
55
56
            return response()->json($response);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

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