1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bruli\EventBusBundle\CommandBus; |
4
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\BusOptionsResolver; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class CommandBus |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ContainerInterface |
12
|
|
|
*/ |
13
|
|
|
private $container; |
14
|
|
|
/** |
15
|
|
|
* @var BusOptionsResolver |
16
|
|
|
*/ |
17
|
|
|
private $optionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* CommandBus constructor. |
21
|
|
|
* |
22
|
|
|
* @param ContainerInterface $container |
23
|
|
|
* @param BusOptionsResolver $optionsResolver |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ContainerInterface $container, BusOptionsResolver $optionsResolver) |
26
|
|
|
{ |
27
|
|
|
$this->container = $container; |
28
|
|
|
$this->optionsResolver = $optionsResolver; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param CommandInterface $command |
33
|
|
|
*/ |
34
|
|
|
public function handle(CommandInterface $command) |
35
|
|
|
{ |
36
|
|
|
$this->handlePreMiddleWares($command); |
37
|
|
|
$this->handleCommand($command); |
38
|
|
|
$this->handlePostMiddleWares($command); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param CommandInterface $command |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
private function handlePreMiddleWares(CommandInterface $command) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$commandName = '\\' . get_class($command); |
47
|
|
|
if (true === $this->optionsResolver->preMiddleWareHasCommands($commandName)) { |
48
|
|
|
foreach ($this->optionsResolver->getPreMiddleWareOptions($commandName) as $preMiddleWareOption) { |
49
|
|
|
$this->container->get($preMiddleWareOption)->handle($command); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param CommandInterface $command |
56
|
|
|
*/ |
57
|
|
|
private function handleCommand(CommandInterface $command) |
58
|
|
|
{ |
59
|
|
|
$this->container->get($this->optionsResolver->getOption('\\' . get_class($command)))->handle($command); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param CommandInterface $command |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
private function handlePostMiddleWares(CommandInterface $command) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$commandName = '\\' . get_class($command); |
68
|
|
|
if (true === $this->optionsResolver->postMiddleWareHasCommands($commandName)) { |
69
|
|
|
foreach ($this->optionsResolver->getPostMiddleWareOptions($commandName) as $postMiddleWareOption) { |
70
|
|
|
$this->container->get($postMiddleWareOption)->handle($command); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.