Passed
Push — master ( 657f5c...767802 )
by Ekin
03:11
created

Handler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 7
dl 0
loc 92
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onStart() 0 14 2
A onHandshake() 0 11 2
A onOpen() 0 10 2
A emit() 0 10 2
A sendConnectedUsersCount() 0 4 1
A onData() 0 4 1
A onClose() 0 6 1
A onStop() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace ekinhbayar\GitAmp\Websocket;
4
5
use function Amp\asyncCall;
6
use Amp\Delayed;
7
use Aerys\Request;
8
use Aerys\Response;
9
use Aerys\Websocket;
10
use Aerys\Websocket\Endpoint;
11
use ekinhbayar\GitAmp\Provider\Listener;
12
use ekinhbayar\GitAmp\Response\Results;
13
use ekinhbayar\GitAmp\Storage\Counter;
14
15
class Handler implements Websocket
16
{
17
    /** @var Endpoint */
18
    private $endpoint;
19
20
    private $counter;
21
22
    private $origin;
23
24
    private $provider;
25
26
    /** @var Results */
27
    private $lastEvents;
28
29 11
    public function __construct(Counter $counter, string $origin, Listener $provider)
30
    {
31 11
        $this->origin   = $origin;
32 11
        $this->counter  = $counter;
33 11
        $this->provider = $provider;
34
    }
35
36 7
    public function onStart(Endpoint $endpoint)
37
    {
38 7
        $this->endpoint = $endpoint;
39
40 7
        $this->counter->set(0);
41
42 7
        asyncCall(function () {
43 7
            while (true) {
44 7
                $this->emit(yield $this->provider->listen());
45
46 4
                yield new Delayed(25000);
47
            }
48 7
        });
49
    }
50
51 2
    public function onHandshake(Request $request, Response $response)
52
    {
53 2
        if ($request->getHeader('origin') !== $this->origin) {
54 1
            $response->setStatus(403);
55 1
            $response->end('<h1>origin not allowed</h1>');
56
57 1
            return null;
58
        }
59
60 1
        return $request->getConnectionInfo()['client_addr'];
61
    }
62
63 3
    public function onOpen(int $clientId, $handshakeData)
64
    {
65 3
        $this->counter->increment();
66
67 3
        $this->sendConnectedUsersCount($this->counter->get());
68
69 3
        if ($this->lastEvents) {
70 1
            $this->endpoint->send($this->lastEvents->jsonEncode(), $clientId);
71
        }
72
    }
73
74 4
    private function emit(Results $events)
75
    {
76 4
        if (!$events->hasEvents()) {
77 2
            return;
78
        }
79
80 2
        $this->lastEvents = $events;
81
82 2
        $this->endpoint->broadcast($events->jsonEncode());
83
    }
84
85 5
    private function sendConnectedUsersCount(int $count)
86
    {
87 5
        $this->endpoint->broadcast(\json_encode(['connectedUsers' => $count]));
88
    }
89
90
    public function onData(int $clientId, Websocket\Message $msg)
91
    {
92
        // yielding $msg buffers the complete payload into a single string.
93
    }
94
95 2
    public function onClose(int $clientId, int $code, string $reason)
96
    {
97 2
        $this->counter->decrement();
98
99 2
        $this->sendConnectedUsersCount($this->counter->get());
100
    }
101
102
    public function onStop()
103
    {
104
        // intentionally left blank
105
    }
106
}
107