SimpleCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
use Amp\Delayed;
4
use PHPinnacle\Ensign\Dispatcher;
5
use PHPinnacle\Ensign\DispatcherBuilder;
6
7
require __DIR__ . '/../vendor/autoload.php';
8
9
class SimpleEvent
10
{
11
    public $num;
12
13
    public function __construct(int $num)
14
    {
15
        $this->num = $num;
16
    }
17
}
18
19
class SimpleCommand
20
{
21
    public $num;
22
    public $delay;
23
24
    public function __construct(int $num, int $delay = 100)
25
    {
26
        $this->num   = $num;
27
        $this->delay = $delay;
28
    }
29
}
30
31
Amp\Loop::run(function () {
32
    $builder = new DispatcherBuilder;
33
    $builder
34
        ->register(SimpleCommand::class, function (SimpleCommand $cmd) {
35
            yield new Delayed($cmd->delay); // Just do some heavy calculations
36
            yield SimpleEvent::class => new SimpleEvent($cmd->num + $cmd->num);
37
38
            yield new Delayed($cmd->delay); // Do more work
39
            yield new SimpleEvent($cmd->num * $cmd->num);
40
41
            return $cmd->num;
42
        })
43
        ->register(SimpleEvent::class, function (SimpleEvent $event) {
44
            echo \sprintf('Signal dispatched with value: %d at %s' . \PHP_EOL, $event->num, \microtime(true));
45
        })
46
    ;
47
48
    $dispatcher = $builder->build();
49
50
    $data = yield $dispatcher->dispatch(new SimpleCommand(10));
51
52
    echo \sprintf('Action resolved with value: %d at %s' . \PHP_EOL, $data, \microtime(true));
53
});
54