|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Application Use Case Class |
|
5
|
|
|
* @author Ticaje <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Ticaje\Hexagonal\Application\UseCase\Bus; |
|
9
|
|
|
|
|
10
|
|
|
use Ticaje\Hexagonal\Application\Signatures\Responder\ResponseInterface; |
|
11
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\BusFacadeInterface; |
|
12
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\ImplementorInterface; |
|
13
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface; |
|
14
|
|
|
use League\Tactician\CommandBus; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Bus |
|
18
|
|
|
* @package Ticaje\Hexagonal\Application\UseCase\Bus |
|
19
|
|
|
* This is the wrapper, the facade to our API that exposes service contract to modules using Command-Bus approach under a Hexagonal context. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Bus implements BusFacadeInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var ImplementorInterface */ |
|
24
|
|
|
private ImplementorInterface $implementor; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array $commands */ |
|
27
|
|
|
private array $commands; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array $handlers */ |
|
30
|
|
|
private array $handlers; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ?CommandBus $bus */ |
|
33
|
|
|
private ?CommandBus $bus; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Bus constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $commands |
|
39
|
|
|
* @param array $handlers |
|
40
|
|
|
* @param ImplementorInterface $implementor |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
array $commands, |
|
44
|
|
|
array $handlers, |
|
45
|
|
|
ImplementorInterface $implementor |
|
46
|
|
|
) { |
|
47
|
|
|
$this->commands = $commands; |
|
48
|
|
|
$this->handlers = $handlers; |
|
49
|
|
|
$this->implementor = $implementor; |
|
50
|
|
|
$this->bus = $this->implementor->provide($this->orchestrate()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function execute(UseCaseCommandInterface $command): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->bus->handle($command); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array |
|
63
|
|
|
* Orchestrating service contract, no virtual types allowed |
|
64
|
|
|
*/ |
|
65
|
|
|
private function orchestrate(): array |
|
66
|
|
|
{ |
|
67
|
|
|
$result = []; |
|
68
|
|
|
foreach ($this->commands as $index => $command) { |
|
69
|
|
|
$result[get_class($command)] = $this->handlers[$index]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.