@@ 7-65 (lines=59) @@ | ||
4 | ||
5 | use InvalidArgumentException; |
|
6 | ||
7 | class DestinationRegistry |
|
8 | { |
|
9 | /** |
|
10 | * @var null|DestinationRegistry |
|
11 | */ |
|
12 | private static $instance = null; |
|
13 | ||
14 | /** |
|
15 | * @var Destination[] |
|
16 | */ |
|
17 | protected $destinations = []; |
|
18 | ||
19 | /** |
|
20 | * @return DestinationRegistry |
|
21 | */ |
|
22 | public static function instance(): DestinationRegistry |
|
23 | { |
|
24 | if (null === static::$instance) { |
|
25 | static::$instance = new DestinationRegistry; |
|
26 | } |
|
27 | ||
28 | return static::$instance; |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @param string $name |
|
33 | * @param Destination $destination |
|
34 | */ |
|
35 | public function set(string $name, Destination $destination): void |
|
36 | { |
|
37 | $this->destinations[$name] = $destination; |
|
38 | } |
|
39 | ||
40 | /** |
|
41 | * @param array $destinations |
|
42 | */ |
|
43 | public function setAll(array $destinations): void |
|
44 | { |
|
45 | $this->destinations = $destinations; |
|
46 | } |
|
47 | ||
48 | /** |
|
49 | * @param string $name |
|
50 | * @return Destination |
|
51 | */ |
|
52 | public function get(string $name): Destination |
|
53 | { |
|
54 | if (!isset($this->destinations[$name])) { |
|
55 | throw new InvalidArgumentException( |
|
56 | sprintf( |
|
57 | 'No destination is registered with name (%s).', |
|
58 | $name |
|
59 | ) |
|
60 | ); |
|
61 | } |
|
62 | ||
63 | return $this->destinations[$name]; |
|
64 | } |
|
65 | } |
|
66 |
@@ 8-67 (lines=60) @@ | ||
5 | ||
6 | use InvalidArgumentException; |
|
7 | ||
8 | class GatewayRegistry |
|
9 | { |
|
10 | /** |
|
11 | * @var null|GatewayRegistry |
|
12 | */ |
|
13 | private static $instance = null; |
|
14 | ||
15 | /** |
|
16 | * @var Gateway[] |
|
17 | */ |
|
18 | protected $gateways = []; |
|
19 | ||
20 | /** |
|
21 | * @return GatewayRegistry |
|
22 | */ |
|
23 | public static function instance(): GatewayRegistry |
|
24 | { |
|
25 | if (null === static::$instance) { |
|
26 | static::$instance = new GatewayRegistry; |
|
27 | } |
|
28 | ||
29 | return static::$instance; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * @param string $destinationType |
|
34 | * @param Gateway $gateway |
|
35 | */ |
|
36 | public function set(string $destinationType, Gateway $gateway): void |
|
37 | { |
|
38 | $this->gateways[$destinationType] = $gateway; |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * @param array $gateways |
|
43 | */ |
|
44 | public function setAll(array $gateways): void |
|
45 | { |
|
46 | $this->gateways = $gateways; |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * @param Destination $destination |
|
51 | * @return Gateway |
|
52 | */ |
|
53 | public function get(Destination $destination): Gateway |
|
54 | { |
|
55 | $destinationType = $destination::type(); |
|
56 | if (!isset($this->gateways[$destinationType])) { |
|
57 | throw new InvalidArgumentException( |
|
58 | sprintf( |
|
59 | 'No gateway for destination type (%s) is supported.', |
|
60 | $destinationType |
|
61 | ) |
|
62 | ); |
|
63 | } |
|
64 | ||
65 | return $this->gateways[$destinationType]; |
|
66 | } |
|
67 | } |
|
68 |