@@ 9-45 (lines=37) @@ | ||
6 | use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler; |
|
7 | use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandlerResolver; |
|
8 | ||
9 | class SimpleArrayResolver implements CommandHandlerResolver |
|
10 | { |
|
11 | /** @var array */ |
|
12 | protected $handlers = []; |
|
13 | ||
14 | /** |
|
15 | * ArrayResolver constructor. |
|
16 | * |
|
17 | * @param array $handlers |
|
18 | */ |
|
19 | public function __construct(array $handlers) |
|
20 | { |
|
21 | $this->handlers = $handlers; |
|
22 | } |
|
23 | ||
24 | /** |
|
25 | * Given a string to identify the Command Handler, return the instance. |
|
26 | * |
|
27 | * @param string $handler |
|
28 | * |
|
29 | * @return CommandHandler |
|
30 | * |
|
31 | * @throws InvalidArgumentException |
|
32 | */ |
|
33 | public function instantiate(string $handler) : CommandHandler |
|
34 | { |
|
35 | if (false === isset($this->handlers[$handler])) { |
|
36 | throw new InvalidArgumentException( |
|
37 | sprintf('Handler %s could not be found. Did you register it?', $handler) |
|
38 | ); |
|
39 | } |
|
40 | ||
41 | $callable = $this->handlers[$handler]; |
|
42 | ||
43 | return ($callable instanceof \Closure) ? $callable() : new $callable(); |
|
44 | } |
|
45 | } |
|
46 |
@@ 9-45 (lines=37) @@ | ||
6 | use NilPortugues\MessageBus\EventBus\Contracts\EventHandler; |
|
7 | use NilPortugues\MessageBus\EventBus\Contracts\EventHandlerResolver; |
|
8 | ||
9 | class SimpleArrayResolver implements EventHandlerResolver |
|
10 | { |
|
11 | /** @var array */ |
|
12 | protected $handlers = []; |
|
13 | ||
14 | /** |
|
15 | * ArrayResolver constructor. |
|
16 | * |
|
17 | * @param array $handlers |
|
18 | */ |
|
19 | public function __construct(array $handlers) |
|
20 | { |
|
21 | $this->handlers = $handlers; |
|
22 | } |
|
23 | ||
24 | /** |
|
25 | * Given a string to identify the Event Handler, return the instance. |
|
26 | * |
|
27 | * @param string $handler |
|
28 | * |
|
29 | * @return EventHandler |
|
30 | * |
|
31 | * @throws InvalidArgumentException |
|
32 | */ |
|
33 | public function instantiate(string $handler) : EventHandler |
|
34 | { |
|
35 | if (false === isset($this->handlers[$handler])) { |
|
36 | throw new InvalidArgumentException( |
|
37 | sprintf('Handler %s could not be found. Did you register it?', $handler) |
|
38 | ); |
|
39 | } |
|
40 | ||
41 | $callable = $this->handlers[$handler]; |
|
42 | ||
43 | return ($callable instanceof \Closure) ? $callable() : new $callable(); |
|
44 | } |
|
45 | } |
|
46 |
@@ 9-45 (lines=37) @@ | ||
6 | use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandler; |
|
7 | use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandlerResolver; |
|
8 | ||
9 | class SimpleArrayResolver implements QueryHandlerResolver |
|
10 | { |
|
11 | /** @var array */ |
|
12 | protected $handlers = []; |
|
13 | ||
14 | /** |
|
15 | * ArrayResolver constructor. |
|
16 | * |
|
17 | * @param array $handlers |
|
18 | */ |
|
19 | public function __construct(array $handlers) |
|
20 | { |
|
21 | $this->handlers = $handlers; |
|
22 | } |
|
23 | ||
24 | /** |
|
25 | * Given a string to identify the Query Handler, return the instance. |
|
26 | * |
|
27 | * @param string $handler |
|
28 | * |
|
29 | * @return QueryHandler |
|
30 | * |
|
31 | * @throws InvalidArgumentException |
|
32 | */ |
|
33 | public function instantiate(string $handler) : QueryHandler |
|
34 | { |
|
35 | if (false === isset($this->handlers[$handler])) { |
|
36 | throw new InvalidArgumentException( |
|
37 | sprintf('Handler %s could not be found. Did you register it?', $handler) |
|
38 | ); |
|
39 | } |
|
40 | ||
41 | $callable = $this->handlers[$handler]; |
|
42 | ||
43 | return ($callable instanceof \Closure) ? $callable() : new $callable(); |
|
44 | } |
|
45 | } |
|
46 |