Passed
Push — master ( ac7767...ce6fb4 )
by PHPinnacle
06:06 queued 03:22
created

Ping::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
use PHPinnacle\Ensign\HandlerRegistry;
4
use PHPinnacle\Ensign\SignalDispatcher;
5
6
require __DIR__ . '/../vendor/autoload.php';
7
8
class Ping
9
{
10
    public $times;
11
12
    public function __construct(int $times)
13
    {
14
        $this->times = $times;
15
    }
16
}
17
18
class Pong extends Ping
19
{
20
}
21
22
class Stop
23
{
24
}
25
26
Amp\Loop::run(function () {
27
    $handlers = new HandlerRegistry();
28
    $handlers
29
        ->register(Ping::class, function (Ping $cmd) {
30
            if ($cmd->times > 0) {
31
                $cmd->times--;
32
33
                echo 'Ping!' . \PHP_EOL;
34
35
                yield Pong::class => new Pong($cmd->times);
36
            } else {
37
                yield Stop::class => new Stop();
38
            }
39
        })
40
        ->register(Pong::class, function (Pong $cmd) {
41
            echo 'Pong!' . \PHP_EOL;
42
43
            yield Ping::class => new Ping($cmd->times);
44
        })
45
        ->register(Stop::class, function () {
46
            echo 'Stop!' . \PHP_EOL;
47
        })
48
    ;
49
50
    $dispatcher = new SignalDispatcher($handlers);
51
52
    yield $dispatcher->dispatch(new Ping(\rand(2, 10)));
53
});
54