|
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
|
138 |
|
public function __construct(array $validBusIds) |
|
28
|
|
|
{ |
|
29
|
138 |
|
foreach ($validBusIds as $validBusId) { |
|
30
|
138 |
|
$this->mapping[$validBusId] = []; |
|
31
|
|
|
} |
|
32
|
138 |
|
} |
|
33
|
|
|
|
|
34
|
42 |
|
public function routeToBus($busId, $commandClassName, $serviceId) |
|
35
|
|
|
{ |
|
36
|
42 |
|
$this->assertValidBusId($busId); |
|
37
|
30 |
|
$this->assertValidCommandFQCN($commandClassName, $serviceId); |
|
38
|
|
|
|
|
39
|
27 |
|
$this->mapping[$busId][$commandClassName] = $serviceId; |
|
40
|
27 |
|
} |
|
41
|
|
|
|
|
42
|
45 |
|
public function routeToAllBuses($commandClassName, $serviceId) |
|
43
|
|
|
{ |
|
44
|
45 |
|
$this->assertValidCommandFQCN($commandClassName, $serviceId); |
|
45
|
|
|
|
|
46
|
45 |
|
foreach($this->mapping as $busId => $mapping) { |
|
47
|
45 |
|
$this->mapping[$busId][$commandClassName] = $serviceId; |
|
48
|
|
|
} |
|
49
|
45 |
|
} |
|
50
|
|
|
|
|
51
|
120 |
|
public function commandToServiceMapping(string $busId): array |
|
52
|
|
|
{ |
|
53
|
120 |
|
$this->assertValidBusId($busId); |
|
54
|
117 |
|
return $this->mapping[$busId]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
135 |
|
private function assertValidBusId(string $busId) |
|
58
|
|
|
{ |
|
59
|
135 |
|
if (!isset($this->mapping[$busId])) { |
|
60
|
15 |
|
throw InvalidCommandBusId::ofName($busId, array_keys($this->mapping)); |
|
61
|
|
|
} |
|
62
|
120 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $commandClassName |
|
66
|
|
|
* @param $serviceId |
|
67
|
|
|
*/ |
|
68
|
69 |
|
protected function assertValidCommandFQCN($commandClassName, $serviceId) |
|
69
|
|
|
{ |
|
70
|
69 |
|
if (!class_exists($commandClassName)) { |
|
71
|
3 |
|
throw new InvalidArgumentException("Can not route $commandClassName to $serviceId, class $commandClassName does not exist!"); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |