Complex classes like SonataNotificationExtension 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 SonataNotificationExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class SonataNotificationExtension extends Extension |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $amqpCounter = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | public function load(array $configs, ContainerBuilder $container) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param array $config |
||
| 91 | */ |
||
| 92 | public function configureClass(ContainerBuilder $container, $config) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $config |
||
| 103 | */ |
||
| 104 | public function configureAdmin(ContainerBuilder $container, $config) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $config |
||
| 113 | */ |
||
| 114 | public function registerParameters(ContainerBuilder $container, $config) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $config |
||
| 122 | */ |
||
| 123 | public function configureBackends(ContainerBuilder $container, $config) |
||
| 151 | |||
| 152 | public function registerDoctrineMapping(array $config) |
||
| 164 | |||
| 165 | protected function checkConfiguration(array $config) |
||
| 179 | |||
| 180 | protected function configureListeners(ContainerBuilder $container, array $config) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param bool $checkLevel |
||
| 203 | * @param int $pause |
||
| 204 | * @param int $maxAge |
||
| 205 | * @param int $batchSize |
||
| 206 | * |
||
| 207 | * @throws \RuntimeException |
||
| 208 | */ |
||
| 209 | protected function configureDoctrineBackends(ContainerBuilder $container, array $config, $checkLevel, $pause, $maxAge, $batchSize) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $manager |
||
| 277 | * @param bool $checkLevel |
||
| 278 | * @param int $pause |
||
| 279 | * @param int $maxAge |
||
| 280 | * @param int $batchSize |
||
| 281 | * @param string $key |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function createDoctrineQueueBackend(ContainerBuilder $container, $manager, $checkLevel, $pause, $maxAge, $batchSize, $key, array $types = []) |
||
| 300 | |||
| 301 | protected function configureRabbitmq(ContainerBuilder $container, array $config) |
||
| 302 | { |
||
| 303 | $queues = $config['queues']; |
||
| 304 | $connection = $config['backends']['rabbitmq']['connection']; |
||
| 305 | $baseExchange = $config['backends']['rabbitmq']['exchange']; |
||
| 306 | $amqBackends = []; |
||
| 307 | |||
| 308 | if (0 === \count($queues)) { |
||
| 309 | $queues = [[ |
||
| 310 | 'queue' => 'default', |
||
| 311 | 'default' => true, |
||
| 312 | 'routing_key' => '', |
||
| 313 | 'recover' => false, |
||
| 314 | 'dead_letter_exchange' => null, |
||
| 315 | 'dead_letter_routing_key' => null, |
||
| 316 | 'ttl' => null, |
||
| 317 | 'prefetch_count' => null, |
||
| 318 | ]]; |
||
| 319 | } |
||
| 320 | |||
| 321 | $deadLetterRoutingKeys = $this->getQueuesParameters('dead_letter_routing_key', $queues); |
||
| 322 | $routingKeys = $this->getQueuesParameters('routing_key', $queues); |
||
| 323 | |||
| 324 | foreach ($deadLetterRoutingKeys as $key) { |
||
| 325 | if (!\in_array($key, $routingKeys, true)) { |
||
| 326 | throw new \RuntimeException(sprintf( |
||
| 327 | 'You must configure the queue having the routing_key "%s" same as dead_letter_routing_key', $key |
||
| 328 | )); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $declaredQueues = []; |
||
| 333 | |||
| 334 | $defaultSet = false; |
||
| 335 | foreach ($queues as $pos => $queue) { |
||
| 336 | if (\in_array($queue['queue'], $declaredQueues, true)) { |
||
| 337 | throw new \RuntimeException('The RabbitMQ backend does not support 2 identicals queue name, please rename one queue'); |
||
| 338 | } |
||
| 339 | |||
| 340 | $declaredQueues[] = $queue['queue']; |
||
| 341 | |||
| 342 | if ($queue['dead_letter_routing_key']) { |
||
| 343 | if (null === $queue['dead_letter_exchange']) { |
||
| 344 | throw new \RuntimeException( |
||
| 345 | 'dead_letter_exchange must be configured when dead_letter_routing_key is set' |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | if (\in_array($queue['routing_key'], $deadLetterRoutingKeys, true)) { |
||
| 351 | $exchange = $this->getAMQPDeadLetterExchangeByRoutingKey($queue['routing_key'], $queues); |
||
| 352 | } else { |
||
| 353 | $exchange = $baseExchange; |
||
| 354 | } |
||
| 355 | |||
| 356 | $id = $this->createAMQPBackend( |
||
| 357 | $container, |
||
| 358 | $exchange, |
||
| 359 | $queue['queue'], |
||
| 360 | $queue['recover'], |
||
| 361 | $queue['routing_key'], |
||
| 362 | $queue['dead_letter_exchange'], |
||
| 363 | $queue['dead_letter_routing_key'], |
||
| 364 | $queue['ttl'], |
||
| 365 | $queue['prefetch_count'] |
||
| 366 | ); |
||
| 367 | |||
| 368 | $amqBackends[$pos] = [ |
||
| 369 | 'type' => $queue['routing_key'], |
||
| 370 | 'backend' => new Reference($id), |
||
| 371 | ]; |
||
| 372 | |||
| 373 | if (true === $queue['default']) { |
||
| 374 | if (true === $defaultSet) { |
||
| 375 | throw new \RuntimeException('You can only set one rabbitmq default queue in your sonata notification configuration.'); |
||
| 376 | } |
||
| 377 | $defaultSet = true; |
||
| 378 | $defaultQueue = $queue['routing_key']; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | if (false === $defaultSet) { |
||
| 383 | throw new \RuntimeException('You need to specify a valid default queue for the rabbitmq backend!'); |
||
| 384 | } |
||
| 385 | |||
| 386 | if (class_exists('\Enqueue\AmqpTools\RabbitMqDlxDelayStrategy')) { |
||
| 387 | $container->setDefinition('sonata.notification.backend.rabbitmq.delay_strategy', new Definition(\Enqueue\AmqpTools\RabbitMqDlxDelayStrategy::class)); |
||
| 388 | } |
||
| 389 | |||
| 390 | $container->getDefinition('sonata.notification.backend.rabbitmq') |
||
| 391 | ->replaceArgument(0, $connection) |
||
| 392 | ->replaceArgument(1, $queues) |
||
| 393 | ->replaceArgument(2, $defaultQueue) |
||
| 394 | ->replaceArgument(3, $amqBackends) |
||
| 395 | ; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $exchange |
||
| 400 | * @param string $name |
||
| 401 | * @param string $recover |
||
| 402 | * @param string $key |
||
| 403 | * @param string $deadLetterExchange |
||
| 404 | * @param string $deadLetterRoutingKey |
||
| 405 | * @param int|null $ttl |
||
| 406 | * @param int|null $prefetchCount |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function createAMQPBackend(ContainerBuilder $container, $exchange, $name, $recover, $key = '', $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param string $name |
||
| 435 | * |
||
| 436 | * @return string[] |
||
| 437 | */ |
||
| 438 | private function getQueuesParameters($name, array $queues) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $key |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | private function getAMQPDeadLetterExchangeByRoutingKey($key, array $queues) |
||
| 465 | } |
||
| 466 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: