Completed
Push — master ( 5e843b...d6e977 )
by Alexey
39:04
created

JoinHandler::broadcastOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SfCod\SocketIoBundle\Service;
4
5
use Psr\Log\LoggerInterface;
6
use SfCod\SocketIoBundle\Events\AbstractEvent;
7
use SfCod\SocketIoBundle\Events\EventInterface;
8
use SfCod\SocketIoBundle\Events\EventPublisherInterface;
9
use SfCod\SocketIoBundle\Events\EventSubscriberInterface;
10
11
class JoinHandler extends AbstractEvent implements EventInterface, EventSubscriberInterface, EventPublisherInterface
12
{
13
    private $broadcast;
14
    private $logger;
15
16
    public function __construct(Broadcast $broadcast, LoggerInterface $logger)
17
    {
18
        $this->broadcast = $broadcast;
19
        $this->logger = $logger;
20
    }
21
22
    /**
23
     * Changel name. For client side this is nsp.
24
     */
25
    public static function broadcastOn(): array
26
    {
27
        return [''];
28
    }
29
30
    public function fire(): array
31
    {
32
        $this->logger->info(json_encode(['type' => 'fire', 'name' => self::name(), 'data' => $this->payload]));
33
34
        return [
35
            'room' => $this->payload['room'],
36
            'socketId' => $this->payload['socketId'],
37
        ];
38
    }
39
40
    /**
41
     * Event name
42
     */
43
    public static function name(): string
44
    {
45
        return 'join';
46
    }
47
48
    /**
49
     * Handle client event
50
     */
51
    public function handle()
52
    {
53
        $this->logger->info(json_encode(['type' => 'handle', 'name' => self::name(), 'data' => $this->payload]));
54
        $this->broadcast->emit(self::name(), [
55
            'room' => $this->payload['room'],
56
            'socketId' => $this->payload['socketId'],
57
        ]);
58
    }
59
}
60