Bus::orchestrate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ticaje\AliexpressConsumer\Application\UseCase;
6
7
use League\Tactician\CommandBus;
8
use League\Tactician\Setup\QuickStart as CommandBusHandlerBootstrapper;
9
10
use Ticaje\AliexpressConsumer\Application\Interfaces\CommandHandlerBusFacadeInterface;
11
use Ticaje\AliexpressConsumer\Application\UseCase\Command\UseCaseCommand;
12
13
/**
14
 * Class Bus
15
 * @package Ticaje\AliexpressConsumer\Application\UseCase
16
 */
17
class Bus implements CommandHandlerBusFacadeInterface
18
{
19
    /** @var CommandBus $bus */
20
    private $bus;
21
22
    /** @var array $commands */
23
    private $commands;
24
25
    /** @var array $handlers */
26
    private $handlers;
27
28
    /**
29
     * Bus constructor.
30
     * @param array $commands
31
     * @param array $handlers
32
     * I will define business deps by means DC.
33
     */
34
    public function __construct(
35
        array $commands,
36
        array $handlers
37
    ) {
38
        $this->commands = $commands;
39
        $this->handlers = $handlers;
40
        $serviceContract = $this->orchestrate();
41
        $this->bus = CommandBusHandlerBootstrapper::create($serviceContract);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function execute(UseCaseCommand $command)
48
    {
49
        return $this->bus->handle($command);
50
    }
51
52
    /**
53
     * @return array
54
     * Orchestrating service contract, no virtual types allowed
55
     */
56
    private function orchestrate()
57
    {
58
        $result = [];
59
        foreach ($this->commands as $index => $command) {
60
            $result[get_class($command)] = $this->handlers[$index];
61
        }
62
        return $result;
63
    }
64
}
65