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 | 16 | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
|
72 | { |
||
73 | 16 | $this->name = $name; |
|
74 | 16 | $this->options = $options; |
|
75 | 16 | $this->cache = $cache; |
|
76 | 16 | $this->logger = $logger; |
|
77 | |||
78 | // get() method used for sdk v2, create methods for v3 |
||
79 | 16 | $useGet = method_exists($client, 'get'); |
|
80 | 16 | $this->sqs = $useGet ? $client->get('Sqs') : $client->createSqs(); |
|
81 | 16 | $this->sns = $useGet ? $client->get('Sns') : $client->createSns(); |
|
82 | 16 | } |
|
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 | 3 | public function publish(array $message, array $options = []) |
|
245 | |||
246 | /** |
||
247 | * {@inheritDoc} |
||
248 | */ |
||
249 | 2 | public function receive(array $options = []) |
|
289 | |||
290 | /** |
||
291 | * {@inheritDoc} |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | 2 | public function delete($id) |
|
314 | |||
315 | /** |
||
316 | * Return the Queue Url |
||
317 | * |
||
318 | * This method relies on in-memory cache and the Cache provider |
||
319 | * to reduce the need to needlessly call the create method on an existing |
||
320 | * Queue. |
||
321 | * |
||
322 | * @return boolean |
||
323 | */ |
||
324 | 9 | public function queueExists() |
|
351 | |||
352 | /** |
||
353 | * Creates an SQS Queue and returns the Queue Url |
||
354 | * |
||
355 | * The create method for SQS Queues is idempotent - if the queue already |
||
356 | * exists, this method will return the Queue Url of the existing Queue. |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | 3 | public function createQueue() |
|
392 | |||
393 | /** |
||
394 | * Creates a Policy for SQS that's required to allow SNS SendMessage access |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 2 | public function createSqsPolicy() |
|
416 | |||
417 | /** |
||
418 | * Checks to see if a Topic exists |
||
419 | * |
||
420 | * This method relies on in-memory cache and the Cache provider |
||
421 | * to reduce the need to needlessly call the create method on an existing |
||
422 | * Topic. |
||
423 | * |
||
424 | * @return boolean |
||
425 | */ |
||
426 | 3 | public function topicExists() |
|
459 | |||
460 | /** |
||
461 | * Creates a SNS Topic and returns the ARN |
||
462 | * |
||
463 | * The create method for the SNS Topics is idempotent - if the topic already |
||
464 | * exists, this method will return the Topic ARN of the existing Topic. |
||
465 | * |
||
466 | * |
||
467 | * @return false|null |
||
468 | */ |
||
469 | 2 | public function createTopic() |
|
486 | |||
487 | /** |
||
488 | * Get a list of Subscriptions for the specified SNS Topic |
||
489 | * |
||
490 | * @param string $topicArn The SNS Topic Arn |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | 4 | public function getTopicSubscriptions($topicArn) |
|
502 | |||
503 | /** |
||
504 | * Subscribes an endpoint to a SNS Topic |
||
505 | * |
||
506 | * @param string $topicArn The ARN of the Topic |
||
507 | * @param string $protocol The protocol of the Endpoint |
||
508 | * @param string $endpoint The Endpoint of the Subscriber |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | 2 | public function subscribeToTopic($topicArn, $protocol, $endpoint) |
|
539 | |||
540 | /** |
||
541 | * Unsubscribes an endpoint from a SNS Topic |
||
542 | * |
||
543 | * The method will return TRUE on success, or FALSE if the Endpoint did not |
||
544 | * have a Subscription on the SNS Topic |
||
545 | * |
||
546 | * @param string $topicArn The ARN of the Topic |
||
547 | * @param string $protocol The protocol of the Endpoint |
||
548 | * @param string $endpoint The Endpoint of the Subscriber |
||
549 | * |
||
550 | * @return Boolean |
||
551 | */ |
||
552 | 1 | public function unsubscribeFromTopic($topicArn, $protocol, $endpoint) |
|
575 | |||
576 | /** |
||
577 | * Handles SNS Notifications |
||
578 | * |
||
579 | * For Subscription notifications, this method will automatically confirm |
||
580 | * the Subscription request |
||
581 | * |
||
582 | * For Message notifications, this method polls the queue and dispatches |
||
583 | * the `{queue}.message_received` event for each message retrieved |
||
584 | * |
||
585 | * @param NotificationEvent $event The Notification Event |
||
586 | * @param string $eventName Name of the event |
||
587 | * @param EventDispatcherInterface $dispatcher |
||
588 | * @return bool|void |
||
589 | */ |
||
590 | 2 | public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
614 | |||
615 | /** |
||
616 | * Removes the message from queue after all other listeners have fired |
||
617 | * |
||
618 | * If an earlier listener has erred or stopped propagation, this method |
||
619 | * will not fire and the Queued Message should become visible in queue again. |
||
620 | * |
||
621 | * Stops Event Propagation after removing the Message |
||
622 | * |
||
623 | * @param MessageEvent $event The SQS Message Event |
||
624 | * @return bool|void |
||
625 | */ |
||
626 | 1 | public function onMessageReceived(MessageEvent $event) |
|
637 | |||
638 | /** |
||
639 | * @param array $options |
||
640 | * @param array $keys |
||
641 | * @return array |
||
642 | */ |
||
643 | 3 | private function getExtraOptions(array $options, array $keys) |
|
647 | } |
||
648 |