Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 8 | class QueueManager extends AbstractManager |
||
| 9 | { |
||
| 10 | const MODULUS_PLACEHOLDER = '{modulus}'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var BindingManager |
||
| 14 | */ |
||
| 15 | private BindingManager $bindingManager; |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * @param BindingManager $bindingManager |
||
| 19 | */ |
||
| 20 | 4 | public function __construct(BindingManager $bindingManager) |
|
| 21 | { |
||
| 22 | 4 | $this->bindingManager = $bindingManager; |
|
| 23 | 4 | } |
|
| 24 | |||
| 25 | /** |
||
| 26 | * @param VhostConfiguration $configuration |
||
| 27 | */ |
||
| 28 | 4 | public function define(VhostConfiguration $configuration): void |
|
| 29 | { |
||
| 30 | 4 | foreach ($configuration->getConfiguration('queues') as $queue) { |
|
| 31 | |||
| 32 | 4 | $name = $queue['name']; |
|
| 33 | 4 | unset($queue['name']); |
|
| 34 | |||
| 35 | 4 | $bindings = $queue['bindings']; |
|
| 36 | 4 | unset($queue['bindings']); |
|
| 37 | |||
| 38 | 4 | $modulus = $queue['modulus']; |
|
| 39 | 4 | unset($queue['modulus']); |
|
| 40 | |||
| 41 | 4 | if (null !== $modulus) { |
|
| 42 | 1 | for ($i = 0; $i < $modulus; $i++) { |
|
| 43 | 1 | $this->createQueue($configuration, $this->getModulusName($name, $i), $queue); |
|
| 44 | |||
| 45 | 1 | $modulusBindings = $bindings; |
|
| 46 | 1 | foreach ($modulusBindings as $key => $binding) { |
|
| 47 | 1 | $modulusBindings[$key]['routing_key'] = $this->getModulusName($binding['routing_key'], $i); |
|
| 48 | 1 | } |
|
| 49 | |||
| 50 | 1 | $this->bindingManager->define( |
|
| 51 | 1 | $configuration, |
|
| 52 | 1 | $this->getModulusName($name, $i), |
|
| 53 | $modulusBindings |
||
| 54 | 1 | ); |
|
| 55 | 1 | } |
|
| 56 | 1 | } else { |
|
| 57 | 4 | $this->createQueue($configuration, $name, $queue); |
|
| 58 | |||
| 59 | 4 | $this->bindingManager->define($configuration, $name, $bindings); |
|
| 60 | } |
||
| 61 | 4 | } |
|
| 62 | 4 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param VhostConfiguration $configuration |
||
| 66 | * @param string $name |
||
| 67 | * @param array $queue |
||
| 68 | */ |
||
| 69 | 4 | private function createQueue(VhostConfiguration $configuration, string $name, array $queue): void |
|
| 70 | { |
||
| 71 | try { |
||
| 72 | 4 | $remoteQueue = $configuration->getClient()->queues()->get($configuration->getName(), $name); |
|
| 73 | 4 | } catch (ClientException $e) { |
|
| 74 | 2 | $this->handleNotFoundException($e); |
|
| 75 | |||
| 76 | 2 | $configuration->getClient()->queues()->create($configuration->getName(), $name, $queue); |
|
| 77 | } |
||
| 78 | |||
| 79 | 4 | if ($configuration->isDeleteAllowed() && isset($remoteQueue) && !$this->isUpToDate($queue, $remoteQueue)) { |
|
| 80 | 1 | $configuration->getClient()->queues()->delete($configuration->getName(), $name); |
|
| 81 | 1 | $configuration->getClient()->queues()->create($configuration->getName(), $name, $queue); |
|
| 82 | 1 | } |
|
| 83 | 4 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @param string $modulus |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 1 | private function getModulusName(string $name, string $modulus): string |
|
| 92 | { |
||
| 93 | 1 | return str_replace(self::MODULUS_PLACEHOLDER, $modulus, $name); |
|
| 94 | } |
||
| 95 | } |
||
| 96 |