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 |
||
59 | public function publish( |
||
60 | $messages, |
||
61 | string $routingKey = '', |
||
62 | string $connectionName = null, |
||
63 | PublishConfig $publishConfig = null |
||
64 | ): void { |
||
65 | $messages = !is_array($messages) ? [$messages] : $messages; |
||
66 | $publishConfig = $publishConfig ?? new PublishConfig(); |
||
67 | |||
68 | $defaultConfig = new Collection($this->manager->getConfig()->get(RabbitMQManager::CONFIG_KEY . ".defaults")); |
||
69 | |||
70 | $connectionName = $connectionName ?? $this->manager->resolveDefaultConfigName(); |
||
71 | $connection = $this->manager->resolveConnection(); |
||
72 | |||
73 | $channelId = $this->manager->resolveChannelId($publishConfig->get("channel_id"), $connectionName); |
||
74 | $channel = $this->manager->resolveChannel($connectionName, $channelId, $connection); |
||
75 | |||
76 | $connectionConfig = $this->manager->resolveConfig($connectionName); |
||
|
|||
77 | |||
78 | // Merge/Override default connection configuration with |
||
79 | // the configuration specified for this publishing. |
||
80 | if ($publishConfig && $publishConfig->getConnectionConfig()) { |
||
81 | // Publish config > Connection config |
||
82 | $connectionConfig = $connectionConfig->merge($publishConfig->getConnectionConfig()); |
||
83 | } |
||
84 | |||
85 | $readyMessages = []; |
||
86 | |||
87 | foreach ($messages as $message) { |
||
88 | // Merge message configuration |
||
89 | // Message config > Publish config > Connection config > Default config |
||
90 | $messageConfig = array_merge( |
||
91 | $defaultConfig->get('message', []), // Default config |
||
92 | $connectionConfig->get('message', []), // Connection config |
||
93 | $publishConfig->get('message', []), // Publish config |
||
94 | $message->getConfig()->toArray(), // Message config |
||
95 | ); |
||
96 | // Override the message config |
||
97 | $message->setConfig($messageConfig); |
||
98 | |||
99 | // Merge the exchange properties |
||
100 | // Publish config > Connection config > Default config |
||
101 | $exchangeProperties = array_merge( |
||
102 | $defaultConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Default properties |
||
103 | $connectionConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Connection properties |
||
104 | $publishConfig->get('exchange', ['properties' => []])['properties'] ?? [], // Publish properties |
||
105 | ); |
||
106 | |||
107 | // Merge the exchange config |
||
108 | // Exchange config > Publish config > Connection config > Default config |
||
109 | $exchangeConfig = array_merge( |
||
110 | $defaultConfig->get('exchange', []), // Default config |
||
111 | $connectionConfig->get('exchange', []), // Connection config |
||
112 | $publishConfig->get('exchange', ['properties' => $exchangeProperties]), // Publish config, |
||
113 | $message->getExchange() ? $message->getExchange()->getConfig()->toArray() : [], // Exchange config |
||
114 | ); |
||
115 | |||
116 | // Merge message exchange configuration |
||
117 | if ($message->getExchange()) { |
||
118 | $message->getExchange()->setConfig($exchangeConfig); |
||
119 | $message->getExchange()->getConfig()->put('name', $message->getExchange()->getName()); |
||
120 | } else { |
||
121 | $message->setExchange(new RabbitMQExchange($exchangeConfig['name'] ?? '', $exchangeConfig)); |
||
122 | } |
||
123 | |||
124 | $readyMessages[] = $message; |
||
125 | } |
||
126 | |||
127 | $this->publishBulk($readyMessages, $channel, $routingKey); |
||
128 | } |
||
192 |