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 | /** |
||
72 | * Flag whether queues should automatically be setup if not available. |
||
73 | * |
||
74 | * @var boolean |
||
75 | */ |
||
76 | private $queueAutoSetup = true; |
||
77 | |||
78 | 15 | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
|
94 | |||
95 | 1 | public function getProvider() |
|
99 | |||
100 | /** |
||
101 | * Builds the configured queues |
||
102 | * |
||
103 | * If a Queue name is passed and configured, this method will build only that |
||
104 | * Queue. |
||
105 | * |
||
106 | * All Create methods are idempotent, if the resource exists, the current ARN |
||
107 | * will be returned |
||
108 | * |
||
109 | */ |
||
110 | 1 | public function create() |
|
137 | |||
138 | /** |
||
139 | * @return Boolean |
||
140 | */ |
||
141 | 1 | public function destroy() |
|
174 | |||
175 | /** |
||
176 | * {@inheritDoc} |
||
177 | * |
||
178 | * This method will either use a SNS Topic to publish a queued message or |
||
179 | * straight to SQS depending on the application configuration. |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | 2 | public function publish(array $message, array $options = []) |
|
239 | |||
240 | /** |
||
241 | * {@inheritDoc} |
||
242 | */ |
||
243 | 2 | public function receive(array $options = []) |
|
283 | |||
284 | /** |
||
285 | * {@inheritDoc} |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 2 | public function delete($id) |
|
308 | |||
309 | /** |
||
310 | * Return the Queue Url |
||
311 | * |
||
312 | * This method relies on in-memory cache and the Cache provider |
||
313 | * to reduce the need to needlessly call the create method on an existing |
||
314 | * Queue. |
||
315 | * |
||
316 | * @return boolean |
||
317 | */ |
||
318 | 9 | public function queueExists() |
|
345 | |||
346 | /** |
||
347 | * Creates an SQS Queue and returns the Queue Url |
||
348 | * |
||
349 | * The create method for SQS Queues is idempotent - if the queue already |
||
350 | * exists, this method will return the Queue Url of the existing Queue. |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | 3 | public function createQueue() |
|
386 | |||
387 | /** |
||
388 | * Creates a Policy for SQS that's required to allow SNS SendMessage access |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | 2 | public function createSqsPolicy() |
|
410 | |||
411 | /** |
||
412 | * Checks to see if a Topic exists |
||
413 | * |
||
414 | * This method relies on in-memory cache and the Cache provider |
||
415 | * to reduce the need to needlessly call the create method on an existing |
||
416 | * Topic. |
||
417 | * |
||
418 | * @return boolean |
||
419 | */ |
||
420 | 3 | public function topicExists() |
|
453 | |||
454 | /** |
||
455 | * Creates a SNS Topic and returns the ARN |
||
456 | * |
||
457 | * The create method for the SNS Topics is idempotent - if the topic already |
||
458 | * exists, this method will return the Topic ARN of the existing Topic. |
||
459 | * |
||
460 | * |
||
461 | * @return false|null |
||
462 | */ |
||
463 | 2 | public function createTopic() |
|
480 | |||
481 | /** |
||
482 | * Get a list of Subscriptions for the specified SNS Topic |
||
483 | * |
||
484 | * @param string $topicArn The SNS Topic Arn |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | 4 | public function getTopicSubscriptions($topicArn) |
|
496 | |||
497 | /** |
||
498 | * Subscribes an endpoint to a SNS Topic |
||
499 | * |
||
500 | * @param string $topicArn The ARN of the Topic |
||
501 | * @param string $protocol The protocol of the Endpoint |
||
502 | * @param string $endpoint The Endpoint of the Subscriber |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | 2 | public function subscribeToTopic($topicArn, $protocol, $endpoint) |
|
533 | |||
534 | /** |
||
535 | * Unsubscribes an endpoint from a SNS Topic |
||
536 | * |
||
537 | * The method will return TRUE on success, or FALSE if the Endpoint did not |
||
538 | * have a Subscription on the SNS Topic |
||
539 | * |
||
540 | * @param string $topicArn The ARN of the Topic |
||
541 | * @param string $protocol The protocol of the Endpoint |
||
542 | * @param string $endpoint The Endpoint of the Subscriber |
||
543 | * |
||
544 | * @return Boolean |
||
545 | */ |
||
546 | 1 | public function unsubscribeFromTopic($topicArn, $protocol, $endpoint) |
|
569 | |||
570 | /** |
||
571 | * Handles SNS Notifications |
||
572 | * |
||
573 | * For Subscription notifications, this method will automatically confirm |
||
574 | * the Subscription request |
||
575 | * |
||
576 | * For Message notifications, this method polls the queue and dispatches |
||
577 | * the `{queue}.message_received` event for each message retrieved |
||
578 | * |
||
579 | * @param NotificationEvent $event The Notification Event |
||
580 | * @param string $eventName Name of the event |
||
581 | * @param EventDispatcherInterface $dispatcher |
||
582 | * @return bool|void |
||
583 | */ |
||
584 | 2 | public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
608 | |||
609 | /** |
||
610 | * Removes the message from queue after all other listeners have fired |
||
611 | * |
||
612 | * If an earlier listener has erred or stopped propagation, this method |
||
613 | * will not fire and the Queued Message should become visible in queue again. |
||
614 | * |
||
615 | * Stops Event Propagation after removing the Message |
||
616 | * |
||
617 | * @param MessageEvent $event The SQS Message Event |
||
618 | * @return bool|void |
||
619 | */ |
||
620 | 1 | public function onMessageReceived(MessageEvent $event) |
|
631 | } |
||
632 |