Complex classes like RabbitServiceProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RabbitServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class RabbitServiceProvider implements ServiceProviderInterface |
||
| 23 | { |
||
| 24 | const DEFAULT_CONNECTION = 'default'; |
||
| 25 | |||
| 26 | public function register(Container $app) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return the name of the connection to use. |
||
| 39 | * |
||
| 40 | * @param array $options Options for the Producer or Consumer. |
||
| 41 | * @param array $connections Connections defined in the config file. |
||
| 42 | * @return string The connection name that will be used |
||
| 43 | */ |
||
| 44 | private function getConnection($app, $options, $connections) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param Container $app |
||
| 57 | */ |
||
| 58 | private function loadConnections($app) |
||
| 59 | { |
||
| 60 | $app['rabbit.connection'] = function ($app) { |
||
| 61 | if (!isset($app['rabbit.connections'])) { |
||
| 62 | throw new \InvalidArgumentException('You need to specify at least a connection in your configuration.'); |
||
| 63 | } |
||
| 64 | |||
| 65 | $connections = []; |
||
| 66 | foreach ($app["rabbit.connections"] as $name => $options) { |
||
| 67 | $amqp_class = "PhpAmqpLib\Connection\AMQPConnection"; |
||
| 68 | $amqp_args = [ |
||
| 69 | $options["host"], |
||
| 70 | $options["port"], |
||
| 71 | $options["user"], |
||
| 72 | $options["password"], |
||
| 73 | $options["vhost"] |
||
| 74 | ]; |
||
| 75 | |||
| 76 | if (isset($options["ssl"]) && true === $options["ssl"]) { |
||
| 77 | $amqp_class = "PhpAmqpLib\Connection\AMQPSSLConnection"; |
||
| 78 | $amqp_args = array_merge( |
||
| 79 | $amqp_args, |
||
| 80 | [ |
||
| 81 | isset($options["ssl_options"]) ? $options["ssl_options"] : ['verify_peer' => false], |
||
| 82 | isset($options["options"]) ? |
||
| 83 | $options["options"] : |
||
| 84 | ['read_write_timeout' => 60,'heartbeat' => 30] |
||
| 85 | ] |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | $reflection = new \ReflectionClass($amqp_class); |
||
| 90 | $connection = $reflection->newInstanceArgs($amqp_args); |
||
| 91 | |||
| 92 | register_shutdown_function( |
||
| 93 | function ($connection) { |
||
| 94 | $connection->close(); |
||
| 95 | }, |
||
| 96 | $connection |
||
| 97 | ); |
||
| 98 | |||
| 99 | $connections[$name] = $connection; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $connections; |
||
| 103 | }; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param Container $app |
||
| 108 | */ |
||
| 109 | private function loadProducers($app) |
||
| 110 | { |
||
| 111 | $app['rabbit.producer'] = function ($app) { |
||
| 112 | if (!isset($app['rabbit.producers'])) { |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | $producers = []; |
||
| 116 | foreach ($app['rabbit.producers'] as $name => $options) { |
||
| 117 | $connection = $this->getConnection($app, $options, $app['rabbit.connections']); |
||
| 118 | |||
| 119 | $producer = new Producer($connection); |
||
| 120 | $producer->setExchangeOptions($options['exchange_options']); |
||
| 121 | |||
| 122 | //this producer doesn't define a queue |
||
| 123 | if (!isset($options['queue_options'])) { |
||
| 124 | $options['queue_options']['name'] = null; |
||
| 125 | } |
||
| 126 | $producer->setQueueOptions($options['queue_options']); |
||
| 127 | |||
| 128 | if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) { |
||
| 129 | $producer->disableAutoSetupFabric(); |
||
| 130 | } |
||
| 131 | |||
| 132 | $producers[$name] = $producer; |
||
| 133 | } |
||
| 134 | |||
| 135 | return $producers; |
||
| 136 | }; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Container $app |
||
| 141 | */ |
||
| 142 | private function loadConsumers($app) |
||
| 143 | { |
||
| 144 | $app['rabbit.consumer'] = function ($app) { |
||
| 145 | if (!isset($app['rabbit.consumers'])) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | $consumers = []; |
||
| 150 | foreach ($app['rabbit.consumers'] as $name => $options) { |
||
| 151 | $connection = $this->getConnection($app, $options, $app['rabbit.connections']); |
||
| 152 | $consumer = new Consumer($connection); |
||
| 153 | $consumer->setExchangeOptions($options['exchange_options']); |
||
| 154 | $consumer->setQueueOptions($options['queue_options']); |
||
| 155 | $consumer->setCallback(array($app[$options['callback']], 'execute')); |
||
| 156 | $consumer = $this->setQosOptions($consumer, $options); |
||
| 157 | |||
| 158 | if (array_key_exists('idle_timeout', $options)) { |
||
| 159 | $consumer->setIdleTimeout($options['idle_timeout']); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) { |
||
| 163 | $consumer->disableAutoSetupFabric(); |
||
| 164 | } |
||
| 165 | |||
| 166 | $consumers[$name] = $consumer; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $consumers; |
||
| 170 | }; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param Container $app |
||
| 175 | */ |
||
| 176 | private function loadAnonymousConsumers($app) |
||
| 177 | { |
||
| 178 | $app['rabbit.anonymous_consumer'] = function ($app) { |
||
| 179 | if (!isset($app['rabbit.anon_consumers'])) { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | $consumers = []; |
||
| 184 | foreach ($app['rabbit.anon_consumers'] as $name => $options) { |
||
| 185 | $connection = $this->getConnection($app, $options, $app['rabbit.connections']); |
||
| 186 | $consumer = new AnonConsumer($connection); |
||
| 187 | $consumer->setExchangeOptions($options['exchange_options']); |
||
| 188 | $consumer->setCallback(array($options['callback'], 'execute')); |
||
| 189 | |||
| 190 | $consumers[$name] = $consumer; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $consumers; |
||
| 194 | }; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param Container $app |
||
| 199 | */ |
||
| 200 | private function loadMultipleConsumers($app) |
||
| 201 | { |
||
| 202 | $app['rabbit.multiple_consumer'] = function ($app) { |
||
| 203 | if (!isset($app['rabbit.multiple_consumers'])) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | $consumers = []; |
||
| 208 | foreach ($app['rabbit.multiple_consumers'] as $name => $options) { |
||
| 209 | $connection = $this->getConnection($app, $options, $app['rabbit.connections']); |
||
| 210 | $consumer = new MultipleConsumer($connection); |
||
| 211 | $consumer->setExchangeOptions($options['exchange_options']); |
||
| 212 | $consumer->setQueues($options['queues']); |
||
| 213 | $consumer = $this->setQosOptions($consumer, $options); |
||
| 214 | |||
| 215 | if (array_key_exists('idle_timeout', $options)) { |
||
| 216 | $consumer->setIdleTimeout($options['idle_timeout']); |
||
| 217 | } |
||
| 218 | |||
| 219 | if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) { |
||
| 220 | $consumer->disableAutoSetupFabric(); |
||
| 221 | } |
||
| 222 | |||
| 223 | $consumers[$name] = $consumer; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $consumers; |
||
| 227 | }; |
||
| 228 | |||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param Container $app |
||
| 233 | */ |
||
| 234 | private function loadRpcClients($app) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Container $app |
||
| 259 | */ |
||
| 260 | private function loadRpcServers($app) |
||
| 261 | { |
||
| 280 | |||
| 281 | private function setQosOptions(BaseConsumer $consumer, $options) |
||
| 293 | } |
||
| 294 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: