RedisBroadcaster   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 72
ccs 9
cts 9
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A auth() 0 11 3
A validAuthenticationResponse() 0 15 2
A broadcast() 0 9 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 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(
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
            $identity,
38 3
            str_replace(['private-', 'presence-'], '', $identity->channel)
39
        );
40
    }
41
42
    /**
43
     * @param Identity $identity
44
     * @param $response
45
     * @return string
46
     */
47
    public function validAuthenticationResponse(Identity $identity, $response)
48
    {
49
        if (is_bool($response)) {
50
            return json_encode($response);
51
        }
52
53
        return json_encode(
54
            [
55
                'channel_data' => [
56
                    'identifier' => $identity->identifier,
57
                    'identity'   => $response,
58
                ]
59
            ]
60
        );
61
    }
62
63
    /**
64
     * Broadcast the given event.
65
     *
66
     * @param  array $channels
67
     * @param  string $event
68
     * @param  array $payload
69
     * @return void
70
     */
71
    public function broadcast(array $channels, $event, array $payload = [])
72
    {
73
        $socket  = isset($payload['socket']) ? $payload['socket'] : null;
74
        $payload = json_encode(['event' => $event, 'data' => $payload, 'socket' => $socket]);
75
76
        foreach ($this->formatChannels($channels) as $channel) {
77
            $this->redis->publish($channel, $payload);
78
        }
79
    }
80
}
81