LaravooleBroadcaster   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A auth() 0 11 3
A validAuthenticationResponse() 0 11 2
A broadcast() 0 14 2
1
<?php
2
3
namespace Laravoole;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
9
10
use Laravoole\LaravooleFacade;
11
12
class LaravooleBroadcaster extends Broadcaster
13
{
14
    /**
15
     * Authenticate the incoming request for a given channel.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @return mixed
19
     */
20
    public function auth($request)
21
    {
22
        if (Str::startsWith($request->channel_name, ['private-', 'presence-']) &&
23
            ! $request->user()) {
24
            throw new HttpException(403);
25
        }
26
27
        return parent::verifyUserCanAccessChannel(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (verifyUserCanAccessChannel() instead of auth()). Are you sure this is correct? If so, you might want to change this to $this->verifyUserCanAccessChannel().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
28
            $request, str_replace(['private-', 'presence-'], '', $request->channel_name)
29
        );
30
    }
31
32
    /**
33
     * Return the valid authentication response.
34
     *
35
     * @param  \Illuminate\Http\Request  $request
36
     * @param  mixed  $result
37
     * @return mixed
38
     */
39
    public function validAuthenticationResponse($request, $result)
40
    {
41
        if (is_bool($result)) {
42
            return json_encode($result);
43
        }
44
45
        return json_encode(['channel_data' => [
46
            'user_id' => $request->user()->getKey(),
47
            'user_info' => $result,
48
        ]]);
49
    }
50
51
    /**
52
     * Broadcast the given event.
53
     *
54
     * @param  array  $channels
55
     * @param  string  $event
56
     * @param  array  $payload
57
     * @return void
58
     */
59
    public function broadcast(array $channels, $event, array $payload = [])
60
    {
61
62
63
        $socket = Arr::pull($payload, 'socket');
64
65
        $payload = [
66
            'event' => $event, 'data' => $payload, 'socket' => $socket,
67
        ];
68
69
        foreach ($this->formatChannels($channels) as $channel) {
70
            LaravooleFacade::task(['channel' => $channel, 'payload' => $payload]);
71
        }
72
    }
73
}
74