Completed
Push — master ( bbeaa5...179311 )
by Edwin
02:39
created

PusherBroadcaster::auth()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 2
b 0
f 0
ccs 3
cts 3
cp 1
cc 3
eloc 6
nc 2
nop 1
crap 3
1
<?php
2
3
namespace EdwinLuijten\Ekko\Broadcast\Broadcasters;
4
5
use EdwinLuijten\Ekko\Broadcast\Identity;
6
use EdwinLuijten\Ekko\Broadcast\StrUtil;
7
use Pusher;
8
9
class PusherBroadcaster extends AbstractBroadcaster implements BroadcasterInterface
10
{
11
    /**
12
     * @var Pusher
13
     */
14
    private $pusher;
15
16
    /**
17
     * PusherBroadcaster constructor.
18 9
     * @param Pusher $pusher
19
     */
20 9
    public function __construct(Pusher $pusher)
21 9
    {
22
        $this->pusher = $pusher;
23
    }
24
25
    /**
26
     * @param Identity $identity
27
     * @return mixed
28
     * @throws \Exception
29
     */
30
    public function auth(Identity $identity)
31 3
    {
32
        if (StrUtil::startsWith($identity->channel, ['private-', 'presence-']) && empty($identity->identifier)) {
33 3
            throw new \Exception('Unauthorized', 403);
34 3
        }
35
36
        return parent::verifyThatIdentityCanAccessChannel(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (verifyThatIdentityCanAccessChannel() instead of auth()). Are you sure this is correct? If so, you might want to change this to $this->verifyThatIdentityCanAccessChannel().

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...
37
            $identity,
38
            str_replace(['private-', 'presence-'], '', $identity->channel)
39
        );
40
    }
41 3
42
    /**
43 3
     * @param Identity $identity
44
     * @param $response
45
     * @return string signature
46
     */
47
    public function validAuthenticationResponse(Identity $identity, $response)
48
    {
49
        if (StrUtil::startsWith($identity->channel, 'private')) {
50
            return $this->pusher->socket_auth($identity->channel, $identity->socketId);
51
        } else {
52
            return $this->pusher->presence_auth(
53
                $identity->channel,
54
                $identity->socketId,
55
                $identity->identifier,
56
                $response
57
            );
58
        }
59
    }
60
61
    /**
62
     * Broadcast the given event.
63
     *
64
     * @param  array $channels
65
     * @param  string $event
66
     * @param  array $payload
67
     * @return void
68
     */
69
    public function broadcast(array $channels, $event, array $payload = [])
70
    {
71
        $socket = isset($payload['socket']) ? $payload['socket'] : null;
72
73
        $this->pusher->trigger($this->formatChannels($channels), $event, $payload, $socket);
74
    }
75
76
    /**
77
     * Get the Pusher instance.
78
     *
79
     * @return Pusher
80
     */
81
    public function getPusher()
82
    {
83
        return $this->pusher;
84
    }
85
}
86