Completed
Branch master (bbeaa5)
by Edwin
02:03
created

RedisBroadcaster::broadcast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
3
namespace EdwinLuijten\Ekko\Broadcast\Broadcasters;
4
5
use Predis\ClientInterface;
6
7
class RedisBroadcaster implements BroadcasterInterface
8
{
9
    /**
10
     * @var ClientInterface
11
     */
12
    private $redis;
13
14
    /**
15
     * RedisBroadcaster constructor.
16
     * @param ClientInterface $redis
17
     */
18 6
    public function __construct(ClientInterface $redis)
19
    {
20 6
        $this->redis = $redis;
21 6
    }
22
23
    /**
24
     * Broadcast the given event.
25
     *
26
     * @param  array $channels
27
     * @param  string $event
28
     * @param  array $payload
29
     * @return void
30
     */
31 3
    public function broadcast(array $channels, $event, array $payload = [])
32
    {
33 3
        $payload = json_encode(['event' => $event, 'data' => $payload]);
34
35 3
        foreach ($channels as $channel) {
36 3
            $this->redis->publish($channel, $payload);
37 2
        }
38 3
    }
39
}
40