1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping; |
5
|
|
|
|
6
|
|
|
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId; |
7
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
final class Routing |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* [ |
13
|
|
|
* 'busId_1' => [ |
14
|
|
|
* 'My\Command\Name1' => 'some.service.id', |
15
|
|
|
* 'My\Other\Command' => 'some.service.id.or.same.one' |
16
|
|
|
* ], |
17
|
|
|
* 'busId_2' => [ |
18
|
|
|
* 'Legacy\App\Command1' => 'some.old.handler', |
19
|
|
|
* ... |
20
|
|
|
* ], |
21
|
|
|
* ] |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $mapping = []; |
26
|
|
|
|
27
|
165 |
|
public function __construct(array $validBusIds) |
28
|
|
|
{ |
29
|
165 |
|
foreach ($validBusIds as $validBusId) { |
30
|
165 |
|
$this->mapping[$validBusId] = []; |
31
|
|
|
} |
32
|
165 |
|
} |
33
|
|
|
|
34
|
45 |
|
public function routeToBus($busId, $commandClassName, $serviceId) |
35
|
|
|
{ |
36
|
45 |
|
$this->assertValidBusId($busId); |
37
|
33 |
|
$this->assertValidCommandFQCN($commandClassName, $serviceId); |
38
|
|
|
|
39
|
30 |
|
$this->mapping[$busId][$commandClassName] = $serviceId; |
40
|
30 |
|
} |
41
|
|
|
|
42
|
57 |
|
public function routeToAllBuses($commandClassName, $serviceId) |
43
|
|
|
{ |
44
|
57 |
|
$this->assertValidCommandFQCN($commandClassName, $serviceId); |
45
|
|
|
|
46
|
57 |
|
foreach($this->mapping as $busId => $mapping) { |
47
|
57 |
|
$this->mapping[$busId][$commandClassName] = $serviceId; |
48
|
|
|
} |
49
|
57 |
|
} |
50
|
|
|
|
51
|
147 |
|
public function commandToServiceMapping(string $busId): array |
52
|
|
|
{ |
53
|
147 |
|
$this->assertValidBusId($busId); |
54
|
144 |
|
return $this->mapping[$busId]; |
55
|
|
|
} |
56
|
|
|
|
57
|
162 |
|
private function assertValidBusId(string $busId) |
58
|
|
|
{ |
59
|
162 |
|
if (!isset($this->mapping[$busId])) { |
60
|
15 |
|
throw InvalidCommandBusId::ofName($busId, array_keys($this->mapping)); |
61
|
|
|
} |
62
|
147 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $commandClassName |
66
|
|
|
* @param $serviceId |
67
|
|
|
*/ |
68
|
81 |
|
protected function assertValidCommandFQCN($commandClassName, $serviceId) |
69
|
|
|
{ |
70
|
81 |
|
if (!class_exists($commandClassName)) { |
71
|
3 |
|
throw new InvalidArgumentException("Can not route $commandClassName to $serviceId, class $commandClassName does not exist!"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |