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
Branch master (dfc5d1)
by Denis
01:12
created

CentrifugeBroadcaster::auth()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace denis660\Centrifuge;
4
5
use denis660\Centrifuge\Contracts\Centrifuge;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, denis660\Centrifuge\Centrifuge.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Exception;
7
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
8
use Illuminate\Broadcasting\BroadcastException;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
class CentrifugeBroadcaster extends Broadcaster
12
{
13
    /**
14
     * The Centrifugo SDK instance.
15
     *
16
     * @var \denis660\Centrifuge\Contracts\Centrifuge
17
     */
18
    protected $centrifugo;
19
20
    /**
21
     * Create a new broadcaster instance.
22
     *
23
     * @param  \denis660\Centrifuge\Contracts\Centrifuge  $centrifugo
24
     */
25
    public function __construct(Centrifuge $centrifugo)
26
    {
27
        $this->centrifugo = $centrifugo;
28
    }
29
30
    /**
31
     * Authenticate the incoming request for a given channel.
32
     *
33
     * @param  \Illuminate\Http\Request  $request
34
     * @return mixed
35
     */
36
    public function auth($request)
37
    {
38
        if ($request->user()) {
39
            $client = $this->getClientFromRequest($request);
40
            $channels = $this->getChannelsFromRequest($request);
41
42
            $response = [];
43
            foreach ($channels as $channel) {
44
                $channelName = $this->getChannelName($channel);
45
46
                try {
47
                    $is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName);
48
                } catch (HttpException $e) {
49
                    $is_access_granted = false;
50
                }
51
52
                $response[$channel] = $this->makeResponseForClient($is_access_granted, $client);
53
            }
54
55
            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...
56
        } else {
57
            throw new HttpException(401);
58
        }
59
    }
60
61
    /**
62
     * Return the valid authentication response.
63
     *
64
     * @param  \Illuminate\Http\Request  $request
65
     * @param  mixed  $result
66
     * @return mixed
67
     */
68
    public function validAuthenticationResponse($request, $result)
69
    {
70
        return $result;
71
    }
72
73
    /**
74
     * Broadcast the given event.
75
     *
76
     * @param  array  $channels
77
     * @param  string  $event
78
     * @param  array  $payload
79
     * @return void
80
     */
81
    public function broadcast(array $channels, $event, array $payload = [])
82
    {
83
        $payload['event'] = $event;
84
85
        $response = $this->centrifugo->broadcast($this->formatChannels($channels), $payload);
86
87
        if (is_array($response) && ! isset($response['error'])) {
88
            return;
89
        }
90
91
        throw new BroadcastException(
92
            $response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error']
93
        );
94
    }
95
96
    /**
97
     * Get client from request.
98
     *
99
     * @param  \Illuminate\Http\Request  $request
100
     * @return string
101
     */
102
    private function getClientFromRequest($request)
103
    {
104
        return $request->get('client', '');
105
    }
106
107
    /**
108
     * Get channels from request.
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @return array
112
     */
113
    private function getChannelsFromRequest($request)
114
    {
115
        $channels = $request->get('channels', []);
116
117
        return is_array($channels) ? $channels : [$channels];
118
    }
119
120
    /**
121
     * Get channel name without $ symbol (if present).
122
     *
123
     * @param  string  $channel
124
     * @return string
125
     */
126
    private function getChannelName(string $channel)
127
    {
128
        return (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel;
129
    }
130
131
    /**
132
     * Make response for client, based on access rights.
133
     *
134
     * @param  bool  $access_granted
135
     * @param  string $client
136
     * @return array
137
     */
138
    private function makeResponseForClient(bool $access_granted, string $client)
139
    {
140
        $info = [];
141
142
        return $access_granted ? [
143
            'sign' => $this->centrifugo->generateConnectionToken($client, 0, $info),
144
            'info' => $info,
145
        ] : [
146
            'status' => 403,
147
        ];
148
    }
149
}
150