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