Completed
Push — master ( 04ceba...2dddc8 )
by Andrii
03:10
created

CommandBus::getExtractor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hiapi\components;
4
5
use League\Tactician\Handler\CommandHandlerMiddleware;
6
use League\Tactician\CommandBus as Worker;
7
use Yii;
8
9
class CommandBus extends \yii\base\Component
10
{
11
    public $extractor;
12
    public $locator;
13
    public $inflector;
14
    public $middlewares = [];
15
16
    protected $worker;
17
18
    protected function buildTactician()
19
    {
20
        $handler = Yii::createObject(CommandHandlerMiddleware::class, [
21
            $this->getExtractor(),
22
            $this->getLocator(),
23
            $this->getInflector(),
24
        ]);
25
26
        $this->registerHandler($handler);
27
28
        return new Worker($this->middlewares);
29
    }
30
31
    public function getExtractor()
32
    {
33
        if (!is_object($this->extractor)) {
34
            $this->extractor = Yii::createObject($this->extractor);
35
        }
36
37
        return $this->extractor;
38
    }
39
40
    public function getLocator()
41
    {
42
        if (!is_object($this->locator)) {
43
            $this->locator = Yii::createObject($this->locator);
44
        }
45
46
        return $this->locator;
47
    }
48
49
    public function getInflector()
50
    {
51
        if (!is_object($this->inflector)) {
52
            $this->inflector = Yii::createObject($this->inflector);
53
        }
54
55
        return $this->inflector;
56
    }
57
58
    public function registerHandler($handler)
59
    {
60
        foreach ($this->middlewares as $middleware) {
61
            if ($middleware instanceof CommandHandlerMiddleware) {
62
                return;
63
            }
64
        }
65
66
        $this->middlewares[] = $handler;
67
    }
68
69
70
    public function handle($command)
71
    {
72
        return $this->getWorker()->handle($command);
73
    }
74
75
    public function getWorker()
76
    {
77
        if ($this->worker === null) {
78
            $this->worker = $this->buildTactician();
79
        }
80
81
        return $this->worker;
82
    }
83
}
84