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

RedisBroadcaster::validAuthenticationResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
3
namespace EdwinLuijten\Ekko\Broadcast\Broadcasters;
4
5
use EdwinLuijten\Ekko\Broadcast\Identity;
6
use EdwinLuijten\Ekko\Broadcast\StrUtil;
7
use Predis\ClientInterface;
8
9
class RedisBroadcaster extends AbstractBroadcaster implements BroadcasterInterface
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    private $redis;
15
16
    /**
17
     * RedisBroadcaster constructor.
18 6
     * @param ClientInterface $redis
19
     */
20 6
    public function __construct(ClientInterface $redis)
21 6
    {
22
        $this->redis = $redis;
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
        }
35 3
36 3
        return parent::verifyThatIdentityCanAccessChannel($identity,
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 2
            str_replace(['private-', 'presence-'], '', $identity->channel));
38 3
    }
39
40
    /**
41
     * @param Identity $identity
42
     * @param $response
43
     * @return string
44
     */
45
    public function validAuthenticationResponse(Identity $identity, $response)
46
    {
47
        if (is_bool($response)) {
48
            return json_encode($response);
49
        }
50
51
        return json_encode([
52
            'channel_data' => [
53
                'identifier' => $identity->identifier,
54
                'identity'   => $response,
55
            ]
56
        ]);
57
    }
58
59
    /**
60
     * Broadcast the given event.
61
     *
62
     * @param  array $channels
63
     * @param  string $event
64
     * @param  array $payload
65
     * @return void
66
     */
67
    public function broadcast(array $channels, $event, array $payload = [])
68
    {
69
        $socket  = isset($payload['socket']) ? $payload['socket'] : null;
70
        $payload = json_encode(['event' => $event, 'data' => $payload, 'socket' => $socket]);
71
72
        foreach ($this->formatChannels($channels) as $channel) {
73
            $this->redis->publish($channel, $payload);
74
        }
75
    }
76
}
77