Complex classes like AwsProvider 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 AwsProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class AwsProvider extends AbstractProvider |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * Aws SQS Client |
||
| 45 | * |
||
| 46 | * @var SqsClient |
||
| 47 | */ |
||
| 48 | private $sqs; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Aws SNS Client |
||
| 52 | * |
||
| 53 | * @var SnsClient |
||
| 54 | */ |
||
| 55 | private $sns; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * SQS Queue URL |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $queueUrl; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * SNS Topic ARN |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $topicArn; |
||
| 70 | |||
| 71 | 15 | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
|
| 72 | { |
||
| 73 | 15 | $this->name = $name; |
|
| 74 | 15 | $this->options = $options; |
|
| 75 | 15 | $this->cache = $cache; |
|
| 76 | 15 | $this->logger = $logger; |
|
| 77 | |||
| 78 | // get() method used for sdk v2, create methods for v3 |
||
| 79 | 15 | $useGet = method_exists($client, 'get'); |
|
| 80 | 15 | $this->sqs = $useGet ? $client->get('Sqs') : $client->createSqs(); |
|
| 81 | 15 | $this->sns = $useGet ? $client->get('Sns') : $client->createSns(); |
|
| 82 | 15 | } |
|
| 83 | |||
| 84 | 1 | public function getProvider() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Builds the configured queues |
||
| 91 | * |
||
| 92 | * If a Queue name is passed and configured, this method will build only that |
||
| 93 | * Queue. |
||
| 94 | * |
||
| 95 | * All Create methods are idempotent, if the resource exists, the current ARN |
||
| 96 | * will be returned |
||
| 97 | * |
||
| 98 | */ |
||
| 99 | 1 | public function create() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return Boolean |
||
| 129 | */ |
||
| 130 | 1 | public function destroy() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritDoc} |
||
| 166 | * |
||
| 167 | * This method will either use a SNS Topic to publish a queued message or |
||
| 168 | * straight to SQS depending on the application configuration. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 2 | public function publish(array $message, array $options = []) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritDoc} |
||
| 233 | */ |
||
| 234 | 2 | public function receive(array $options = []) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritDoc} |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 2 | public function delete($id) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Return the Queue Url |
||
| 302 | * |
||
| 303 | * This method relies on in-memory cache and the Cache provider |
||
| 304 | * to reduce the need to needlessly call the create method on an existing |
||
| 305 | * Queue. |
||
| 306 | * |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | 9 | public function queueExists() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Creates an SQS Queue and returns the Queue Url |
||
| 339 | * |
||
| 340 | * The create method for SQS Queues is idempotent - if the queue already |
||
| 341 | * exists, this method will return the Queue Url of the existing Queue. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 3 | public function createQueue() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Creates a Policy for SQS that's required to allow SNS SendMessage access |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | 2 | public function createSqsPolicy() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Checks to see if a Topic exists |
||
| 404 | * |
||
| 405 | * This method relies on in-memory cache and the Cache provider |
||
| 406 | * to reduce the need to needlessly call the create method on an existing |
||
| 407 | * Topic. |
||
| 408 | * |
||
| 409 | * @return boolean |
||
| 410 | */ |
||
| 411 | 3 | public function topicExists() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Creates a SNS Topic and returns the ARN |
||
| 447 | * |
||
| 448 | * The create method for the SNS Topics is idempotent - if the topic already |
||
| 449 | * exists, this method will return the Topic ARN of the existing Topic. |
||
| 450 | * |
||
| 451 | * |
||
| 452 | * @return false|null |
||
| 453 | */ |
||
| 454 | 2 | public function createTopic() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Get a list of Subscriptions for the specified SNS Topic |
||
| 474 | * |
||
| 475 | * @param string $topicArn The SNS Topic Arn |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | 4 | public function getTopicSubscriptions($topicArn) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Subscribes an endpoint to a SNS Topic |
||
| 490 | * |
||
| 491 | * @param string $topicArn The ARN of the Topic |
||
| 492 | * @param string $protocol The protocol of the Endpoint |
||
| 493 | * @param string $endpoint The Endpoint of the Subscriber |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | 2 | public function subscribeToTopic($topicArn, $protocol, $endpoint) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Unsubscribes an endpoint from a SNS Topic |
||
| 527 | * |
||
| 528 | * The method will return TRUE on success, or FALSE if the Endpoint did not |
||
| 529 | * have a Subscription on the SNS Topic |
||
| 530 | * |
||
| 531 | * @param string $topicArn The ARN of the Topic |
||
| 532 | * @param string $protocol The protocol of the Endpoint |
||
| 533 | * @param string $endpoint The Endpoint of the Subscriber |
||
| 534 | * |
||
| 535 | * @return Boolean |
||
| 536 | */ |
||
| 537 | 1 | public function unsubscribeFromTopic($topicArn, $protocol, $endpoint) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Handles SNS Notifications |
||
| 563 | * |
||
| 564 | * For Subscription notifications, this method will automatically confirm |
||
| 565 | * the Subscription request |
||
| 566 | * |
||
| 567 | * For Message notifications, this method polls the queue and dispatches |
||
| 568 | * the `{queue}.message_received` event for each message retrieved |
||
| 569 | * |
||
| 570 | * @param NotificationEvent $event The Notification Event |
||
| 571 | * @param string $eventName Name of the event |
||
| 572 | * @param EventDispatcherInterface $dispatcher |
||
| 573 | * @return bool|void |
||
| 574 | */ |
||
| 575 | 2 | public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Removes the message from queue after all other listeners have fired |
||
| 602 | * |
||
| 603 | * If an earlier listener has erred or stopped propagation, this method |
||
| 604 | * will not fire and the Queued Message should become visible in queue again. |
||
| 605 | * |
||
| 606 | * Stops Event Propagation after removing the Message |
||
| 607 | * |
||
| 608 | * @param MessageEvent $event The SQS Message Event |
||
| 609 | * @return bool|void |
||
| 610 | */ |
||
| 611 | 1 | public function onMessageReceived(MessageEvent $event) |
|
| 622 | } |
||
| 623 |