Conditions | 1 |
Paths | 1 |
Total Lines | 135 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
185 | private function eventBus(ContainerBuilder $container, $user) |
||
186 | { |
||
187 | $busId = 'bengor.user.simple_bus_' . $user . '_event_bus'; |
||
188 | $middlewareTag = 'bengor_user_' . $user . '_event_bus_middleware'; |
||
189 | $subscriberTag = 'bengor_user_' . $user . '_event_subscriber'; |
||
190 | |||
191 | // Define the event bus for the given user type |
||
192 | // The middleware tag string will be later used to add |
||
193 | // required middleware to this specific event bus |
||
194 | $container->setDefinition( |
||
195 | $busId, |
||
196 | (new Definition( |
||
197 | MessageBusSupportingMiddleware::class |
||
198 | ))->addTag('message_bus', [ |
||
199 | 'type' => 'event', |
||
200 | 'middleware_tag' => $middlewareTag, |
||
201 | ])->setPublic(false) |
||
202 | ); |
||
203 | |||
204 | // Find services tagged with $middlewareTag string and add |
||
205 | // them to the current user type's event bus |
||
206 | (new ConfigureMiddlewares($busId, $middlewareTag))->process($container); |
||
207 | |||
208 | // Declares callable resolver for the current user type's event bus |
||
209 | $container->setDefinition( |
||
210 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.callable_resolver', |
||
211 | (new Definition( |
||
212 | ServiceLocatorAwareCallableResolver::class, [ |
||
213 | [ |
||
214 | new Reference('service_container'), 'get', |
||
215 | ], |
||
216 | ] |
||
217 | ))->setPublic(false) |
||
218 | ); |
||
219 | |||
220 | // Declares class based event name resolver for the current user type's event bus |
||
221 | $container->setDefinition( |
||
222 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.class_based_event_name_resolver', |
||
223 | (new Definition( |
||
224 | ClassBasedNameResolver::class |
||
225 | ))->setPublic(false) |
||
226 | ); |
||
227 | |||
228 | // Declares the event subscribers collection for the current user type's event bus, |
||
229 | // will contain the association between events an subscribers |
||
230 | $container->setDefinition( |
||
231 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.event_subscribers_collection', |
||
232 | (new Definition( |
||
233 | CallableCollection::class, [ |
||
234 | [], |
||
235 | $container->getDefinition( |
||
236 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.callable_resolver' |
||
237 | ), |
||
238 | ] |
||
239 | ))->setPublic(false) |
||
240 | ); |
||
241 | |||
242 | // Declares the subscriber resolver with the NameResolver |
||
243 | // strategy and SubscriberCollection key values for Event => Subscriber |
||
244 | $container->setDefinition( |
||
245 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.event_subscribers_resolver', |
||
246 | (new Definition( |
||
247 | NameBasedMessageSubscriberResolver::class, [ |
||
248 | $container->getDefinition( |
||
249 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.class_based_event_name_resolver' |
||
250 | ), |
||
251 | $container->getDefinition( |
||
252 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.event_subscribers_collection' |
||
253 | ), |
||
254 | ] |
||
255 | ))->setPublic(false) |
||
256 | ); |
||
257 | |||
258 | // Declares the Subscriber |
||
259 | $container |
||
260 | ->findDefinition( |
||
261 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.delegates_to_message_handler_middleware' |
||
262 | ) |
||
263 | ->addArgument( |
||
264 | $container->getDefinition( |
||
265 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.event_subscribers_resolver' |
||
266 | ) |
||
267 | )->setPublic(false); |
||
268 | |||
269 | // Declares the tag that will be used to associate the |
||
270 | // subscribers to the current user type's event bus |
||
271 | (new RegisterSubscribers( |
||
272 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.event_subscribers_collection', |
||
273 | $subscriberTag, |
||
274 | 'subscribes_to' |
||
275 | ))->process($container); |
||
276 | |||
277 | // Decorate SimpleBus' event bus with BenGorUser's event bus |
||
278 | $container->setDefinition( |
||
279 | 'bengor_user.' . $user . '.event_bus', |
||
280 | new Definition( |
||
281 | SimpleBusUserEventBus::class, [ |
||
282 | $container->getDefinition($busId), |
||
283 | ] |
||
284 | ) |
||
285 | ); |
||
286 | |||
287 | // All about aggregate recorded message |
||
288 | $container->setDefinition( |
||
289 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.aggregates_recorded_messages', |
||
290 | new Definition( |
||
291 | AggregatesRecordedMessages::class, [ |
||
292 | [], |
||
293 | ] |
||
294 | ) |
||
295 | )->setPublic(false); |
||
296 | $container |
||
297 | ->findDefinition( |
||
298 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.handles_recorded_messages_middleware' |
||
299 | ) |
||
300 | ->setArguments([ |
||
301 | $container->getDefinition( |
||
302 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.aggregates_recorded_messages' |
||
303 | ), |
||
304 | $container->getDefinition($busId), |
||
305 | ])->setPublic(false); |
||
306 | (new RegisterMessageRecorders( |
||
307 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.aggregates_recorded_messages', |
||
308 | 'event_recorder' |
||
309 | ))->process($container); |
||
310 | |||
311 | CompilerPassUtil::prependBeforeOptimizationPass( |
||
312 | $container, |
||
313 | new AddMiddlewareTags( |
||
314 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_event_bus.handles_recorded_messages_middleware', |
||
315 | ['command'], |
||
316 | 200 |
||
317 | ) |
||
318 | ); |
||
319 | } |
||
320 | } |
||
321 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.