| Conditions | 7 |
| Paths | 12 |
| Total Lines | 69 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | public function publish( |
||
| 59 | $messages, |
||
| 60 | string $routingKey = '', |
||
| 61 | string $connectionName = null, |
||
| 62 | PublishConfig $publishConfig = null |
||
| 63 | ): void { |
||
| 64 | $messages = !is_array($messages) ? [$messages] : $messages; |
||
| 65 | $publishConfig = $publishConfig ?? new PublishConfig(); |
||
| 66 | |||
| 67 | $defaultConfig = new Collection($this->manager->getConfig()->get(RabbitMQManager::CONFIG_KEY . '.defaults')); |
||
| 68 | |||
| 69 | $connectionName = $connectionName ?? $this->manager->resolveDefaultConfigName(); |
||
| 70 | $connection = $this->manager->resolveConnection(); |
||
| 71 | |||
| 72 | $channelId = $this->manager->resolveChannelId($publishConfig->get('channel_id'), $connectionName); |
||
| 73 | $channel = $this->manager->resolveChannel($connectionName, $channelId, $connection); |
||
| 74 | |||
| 75 | $connectionConfig = $this->manager->resolveConfig($connectionName); |
||
| 76 | |||
| 77 | // Merge/Override default connection configuration with |
||
| 78 | // the configuration specified for this publishing. |
||
| 79 | if ($publishConfig && $publishConfig->getConnectionConfig()) { |
||
| 80 | // Publish config > Connection config |
||
| 81 | $connectionConfig = $connectionConfig->merge($publishConfig->getConnectionConfig()); |
||
| 82 | } |
||
| 83 | |||
| 84 | $readyMessages = []; |
||
| 85 | |||
| 86 | foreach ($messages as $message) { |
||
| 87 | // Merge message configuration |
||
| 88 | // Message config > Publish config > Connection config > Default config |
||
| 89 | $messageConfig = array_merge( |
||
| 90 | $defaultConfig->get('message', []), // Default config |
||
| 91 | $connectionConfig->get('message', []), // Connection config |
||
| 92 | $publishConfig->get('message', []), // Publish config |
||
| 93 | $message->getConfig()->toArray(), // Message config |
||
| 94 | ); |
||
| 95 | // Override the message config |
||
| 96 | $message->setConfig($messageConfig); |
||
| 97 | |||
| 98 | // Merge the exchange properties |
||
| 99 | // Publish config > Connection config > Default config |
||
| 100 | $exchangeProperties = array_merge( |
||
| 101 | $defaultConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Default properties |
||
| 102 | $connectionConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Connection properties |
||
| 103 | $publishConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Publish properties |
||
| 104 | ); |
||
| 105 | |||
| 106 | // Merge the exchange config |
||
| 107 | // Exchange config > Publish config > Connection config > Default config |
||
| 108 | $exchangeConfig = array_merge( |
||
| 109 | $defaultConfig->get('exchange', []), // Default config |
||
| 110 | $connectionConfig->get('exchange', []), // Connection config |
||
| 111 | $publishConfig->get('exchange', ['properties' => $exchangeProperties]), // Publish config, |
||
| 112 | $message->getExchange() ? $message->getExchange()->getConfig()->toArray() : [], // Exchange config |
||
| 113 | ); |
||
| 114 | |||
| 115 | // Merge message exchange configuration |
||
| 116 | if ($message->getExchange()) { |
||
| 117 | $message->getExchange()->setConfig($exchangeConfig); |
||
| 118 | $message->getExchange()->getConfig()->put('name', $message->getExchange()->getName()); |
||
| 119 | } else { |
||
| 120 | $message->setExchange(new RabbitMQExchange($exchangeConfig['name'] ?? '', $exchangeConfig)); |
||
| 121 | } |
||
| 122 | |||
| 123 | $readyMessages[] = $message; |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->publishBulk($readyMessages, $channel, $routingKey); |
||
| 127 | } |
||
| 193 |