| Conditions | 3 |
| Paths | 4 |
| Total Lines | 105 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 73 | private function commandBus(ContainerBuilder $container, $user, $isApi = false) |
||
| 74 | { |
||
| 75 | $apiPartName = $isApi ? '_api' : ''; |
||
| 76 | $busId = 'bengor.user.simple_bus_' . $user . $apiPartName . '_command_bus'; |
||
| 77 | $middlewareTag = 'bengor_user_' . $user . '_command_bus_middleware'; |
||
| 78 | $handlerTag = 'bengor_user_' . $user . $apiPartName . '_command_bus_handler'; |
||
| 79 | |||
| 80 | // Define the command bus for the given user type |
||
| 81 | // The middleware tag string will be later used to add |
||
| 82 | // required middleware to this specific command bus |
||
| 83 | $container->setDefinition( |
||
| 84 | $busId, |
||
| 85 | (new Definition( |
||
| 86 | MessageBusSupportingMiddleware::class |
||
| 87 | ))->addTag('message_bus', [ |
||
| 88 | 'type' => 'command', |
||
| 89 | 'middleware_tag' => $middlewareTag, |
||
| 90 | ])->setPublic(false) |
||
| 91 | ); |
||
| 92 | |||
| 93 | // Find services tagged with $middlewareTag string and add |
||
| 94 | // them to the current user type's command bus |
||
| 95 | (new ConfigureMiddlewares($busId, $middlewareTag))->process($container); |
||
| 96 | |||
| 97 | // Declares callable resolver for the current user type's command bus |
||
| 98 | $container->setDefinition( |
||
| 99 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.callable_resolver', |
||
| 100 | (new Definition( |
||
| 101 | ServiceLocatorAwareCallableResolver::class, [ |
||
| 102 | [ |
||
| 103 | new Reference('service_container'), 'get', |
||
| 104 | ], |
||
| 105 | ] |
||
| 106 | ))->setPublic(false) |
||
| 107 | ); |
||
| 108 | |||
| 109 | // Declares class based command name resolver for the current user type's command bus |
||
| 110 | $container->setDefinition( |
||
| 111 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.class_based_command_name_resolver', |
||
| 112 | (new Definition( |
||
| 113 | ClassBasedNameResolver::class |
||
| 114 | ))->setPublic(false) |
||
| 115 | ); |
||
| 116 | |||
| 117 | // Declares the handler map for the current user type's command bus, |
||
| 118 | // will contain the association between commands an handlers |
||
| 119 | $container->setDefinition( |
||
| 120 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.command_handler_map', |
||
| 121 | (new Definition( |
||
| 122 | CallableMap::class, [ |
||
| 123 | [], |
||
| 124 | $container->getDefinition( |
||
| 125 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.callable_resolver' |
||
| 126 | ), |
||
| 127 | ] |
||
| 128 | ))->setPublic(false) |
||
| 129 | ); |
||
| 130 | |||
| 131 | // Declares the handler resolver with the NameResolver |
||
| 132 | // strategy and HandlerMap key values for Command => Handler |
||
| 133 | $container->setDefinition( |
||
| 134 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.command_handler_resolver', |
||
| 135 | (new Definition( |
||
| 136 | NameBasedMessageHandlerResolver::class, [ |
||
| 137 | $container->getDefinition( |
||
| 138 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.class_based_command_name_resolver' |
||
|
|
|||
| 139 | ), |
||
| 140 | $container->getDefinition( |
||
| 141 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.command_handler_map' |
||
| 142 | ), |
||
| 143 | ] |
||
| 144 | ))->setPublic(false) |
||
| 145 | ); |
||
| 146 | |||
| 147 | // Declares the Handler |
||
| 148 | $container |
||
| 149 | ->findDefinition( |
||
| 150 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.delegates_to_message_handler_middleware' |
||
| 151 | ) |
||
| 152 | ->addArgument( |
||
| 153 | $container->getDefinition( |
||
| 154 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.command_handler_resolver' |
||
| 155 | ) |
||
| 156 | )->setPublic(false); |
||
| 157 | |||
| 158 | // Declares the tag that will be used to associate the |
||
| 159 | // handlers to the current user type's command bus |
||
| 160 | (new RegisterHandlers( |
||
| 161 | 'bengor_user.simple_bus_bridge_bundle.' . $user . '_command_bus.command_handler_map', |
||
| 162 | $handlerTag, |
||
| 163 | 'handles' |
||
| 164 | ))->process($container); |
||
| 165 | |||
| 166 | // Decorate SimpleBus' command bus with BenGorUser's command bus |
||
| 167 | |||
| 168 | $apiPartName = $isApi ? '.api_' : '.'; |
||
| 169 | $container->setDefinition( |
||
| 170 | 'bengor_user.' . $user . $apiPartName . 'command_bus', |
||
| 171 | new Definition( |
||
| 172 | SimpleBusUserCommandBus::class, [ |
||
| 173 | $container->getDefinition($busId), |
||
| 174 | ] |
||
| 175 | ) |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 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.