Ping::__construct()   A
last analyzed

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\DispatcherBuilder;
4
5
require __DIR__ . '/../vendor/autoload.php';
6
7
class Ping
8
{
9
    public $times;
10
11
    public function __construct(int $times)
12
    {
13
        $this->times = $times;
14
    }
15
}
16
17
class Pong extends Ping
18
{
19
}
20
21
class Stop
22
{
23
}
24
25
Amp\Loop::run(function () {
26
    $builder = new DispatcherBuilder;
27
    $builder
28
        ->register(Ping::class, function (Ping $cmd) {
29
            if ($cmd->times > 0) {
30
                $cmd->times--;
31
32
                echo 'Ping!' . \PHP_EOL;
33
34
                yield new Pong($cmd->times);
35
            } else {
36
                yield new Stop();
37
            }
38
        })
39
        ->register(Pong::class, function (Pong $cmd) {
40
            echo 'Pong!' . \PHP_EOL;
41
42
            yield new Ping($cmd->times);
43
        })
44
        ->register(Stop::class, function () {
45
            echo 'Stop!' . \PHP_EOL;
46
        })
47
    ;
48
49
    $dispatcher = $builder->build();
50
51
    yield $dispatcher->dispatch(new Ping(\rand(2, 10)));
52
});
53