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) |
|
| 83 | |||
| 84 | 1 | public function getProvider() |
|
| 85 | { |
||
| 86 | 1 | return "AWS"; |
|
| 87 | } |
||
| 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() |
|
| 131 | { |
||
| 132 | 1 | $key = $this->getNameWithPrefix() . '_url'; |
|
| 133 | 1 | $this->cache->delete($key); |
|
| 134 | |||
| 135 | 1 | if ($this->queueExists()) { |
|
| 136 | // Delete the SQS Queue |
||
| 137 | 1 | $this->sqs->deleteQueue([ |
|
| 138 | 1 | 'QueueUrl' => $this->queueUrl |
|
| 139 | ]); |
||
| 140 | |||
| 141 | 1 | $this->log(200,"SQS Queue removed", ['QueueUrl' => $this->queueUrl]); |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | $key = $this->getNameWithPrefix() . '_arn'; |
|
| 145 | 1 | $this->cache->delete($key); |
|
| 146 | |||
| 147 | 1 | if ($this->topicExists() || !empty($this->queueUrl)) { |
|
| 148 | // Delete the SNS Topic |
||
| 149 | 1 | $topicArn = !empty($this->topicArn) |
|
| 150 | 1 | ? $this->topicArn |
|
| 151 | 1 | : str_replace('sqs', 'sns', $this->queueUrl) |
|
| 152 | ; |
||
| 153 | |||
| 154 | 1 | $this->sns->deleteTopic([ |
|
| 155 | 1 | 'TopicArn' => $topicArn |
|
| 156 | ]); |
||
| 157 | |||
| 158 | 1 | $this->log(200,"SNS Topic removed", ['TopicArn' => $topicArn]); |
|
| 159 | } |
||
| 160 | |||
| 161 | 1 | return true; |
|
| 162 | } |
||
| 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 = []) |
|
| 173 | { |
||
| 174 | 2 | $options = $this->mergeOptions($options); |
|
| 175 | 2 | $publishStart = microtime(true); |
|
| 176 | |||
| 177 | // ensures that the SQS Queue and SNS Topic exist |
||
| 178 | 2 | if (!$this->queueExists()) { |
|
| 179 | $this->create(); |
||
| 180 | } |
||
| 181 | |||
| 182 | 2 | if ($options['push_notifications']) { |
|
| 183 | |||
| 184 | 1 | if (!$this->topicExists()) { |
|
| 185 | $this->create(); |
||
| 186 | } |
||
| 187 | |||
| 188 | $message = [ |
||
| 189 | 1 | 'default' => $this->getNameWithPrefix(), |
|
| 190 | 1 | 'sqs' => json_encode($message), |
|
| 191 | 1 | 'http' => $this->getNameWithPrefix(), |
|
| 192 | 1 | 'https' => $this->getNameWithPrefix(), |
|
| 193 | ]; |
||
| 194 | |||
| 195 | 1 | $result = $this->sns->publish([ |
|
| 196 | 1 | 'TopicArn' => $this->topicArn, |
|
| 197 | 1 | 'Subject' => $this->getName(), |
|
| 198 | 1 | 'Message' => json_encode($message), |
|
| 199 | 1 | 'MessageStructure' => 'json' |
|
| 200 | ]); |
||
| 201 | |||
| 202 | $context = [ |
||
| 203 | 1 | 'TopicArn' => $this->topicArn, |
|
| 204 | 1 | 'MessageId' => $result->get('MessageId'), |
|
| 205 | 1 | 'push_notifications' => $options['push_notifications'], |
|
| 206 | 1 | 'publish_time' => microtime(true) - $publishStart |
|
| 207 | ]; |
||
| 208 | 1 | $this->log(200,"Message published to SNS", $context); |
|
| 209 | |||
| 210 | 1 | return $result->get('MessageId'); |
|
| 211 | } |
||
| 212 | |||
| 213 | 1 | $result = $this->sqs->sendMessage([ |
|
| 214 | 1 | 'QueueUrl' => $this->queueUrl, |
|
| 215 | 1 | 'MessageBody' => json_encode($message), |
|
| 216 | 1 | 'DelaySeconds' => $options['message_delay'] |
|
| 217 | ]); |
||
| 218 | |||
| 219 | $context = [ |
||
| 220 | 1 | 'QueueUrl' => $this->queueUrl, |
|
| 221 | 1 | 'MessageId' => $result->get('MessageId'), |
|
| 222 | 1 | 'push_notifications' => $options['push_notifications'] |
|
| 223 | ]; |
||
| 224 | 1 | $this->log(200,"Message published to SQS", $context); |
|
| 225 | |||
| 226 | 1 | return $result->get('MessageId'); |
|
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritDoc} |
||
| 231 | */ |
||
| 232 | 2 | public function receive(array $options = []) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritDoc} |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | 2 | public function delete($id) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return the Queue Url |
||
| 300 | * |
||
| 301 | * This method relies on in-memory cache and the Cache provider |
||
| 302 | * to reduce the need to needlessly call the create method on an existing |
||
| 303 | * Queue. |
||
| 304 | * |
||
| 305 | * @return boolean |
||
| 306 | */ |
||
| 307 | 9 | public function queueExists() |
|
| 308 | { |
||
| 309 | 9 | if (isset($this->queueUrl)) { |
|
| 310 | 3 | return true; |
|
| 311 | } |
||
| 312 | |||
| 313 | 7 | $key = $this->getNameWithPrefix() . '_url'; |
|
| 314 | 7 | if ($this->cache->contains($key)) { |
|
| 315 | 1 | $this->queueUrl = $this->cache->fetch($key); |
|
| 316 | |||
| 317 | 1 | return true; |
|
| 318 | } |
||
| 319 | |||
| 320 | try { |
||
| 321 | 6 | $result = $this->sqs->getQueueUrl([ |
|
| 322 | 6 | 'QueueName' => $this->getNameWithPrefix() |
|
| 323 | ]); |
||
| 324 | |||
| 325 | 6 | if ($this->queueUrl = $result->get('QueueUrl')) { |
|
| 326 | 6 | $this->cache->save($key, $this->queueUrl); |
|
| 327 | |||
| 328 | 6 | return true; |
|
| 329 | } |
||
| 330 | } catch (SqsException $e) {} |
||
|
|
|||
| 331 | |||
| 332 | return false; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Creates an SQS Queue and returns the Queue Url |
||
| 337 | * |
||
| 338 | * The create method for SQS Queues is idempotent - if the queue already |
||
| 339 | * exists, this method will return the Queue Url of the existing Queue. |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 3 | public function createQueue() |
|
| 344 | { |
||
| 345 | 3 | $result = $this->sqs->createQueue([ |
|
| 346 | 3 | 'QueueName' => $this->getNameWithPrefix(), |
|
| 347 | 'Attributes' => [ |
||
| 348 | 3 | 'VisibilityTimeout' => $this->options['message_timeout'], |
|
| 349 | 3 | 'MessageRetentionPeriod' => $this->options['message_expiration'], |
|
| 350 | 3 | 'ReceiveMessageWaitTimeSeconds' => $this->options['receive_wait_time'] |
|
| 351 | ] |
||
| 352 | ]); |
||
| 353 | |||
| 354 | 3 | $this->queueUrl = $result->get('QueueUrl'); |
|
| 355 | |||
| 356 | 3 | $key = $this->getNameWithPrefix() . '_url'; |
|
| 357 | 3 | $this->cache->save($key, $this->queueUrl); |
|
| 358 | |||
| 359 | 3 | $this->log(200, "Created SQS Queue", ['QueueUrl' => $this->queueUrl]); |
|
| 360 | |||
| 361 | 3 | if ($this->options['push_notifications']) { |
|
| 362 | |||
| 363 | 2 | $policy = $this->createSqsPolicy(); |
|
| 364 | |||
| 365 | 2 | $this->sqs->setQueueAttributes([ |
|
| 366 | 2 | 'QueueUrl' => $this->queueUrl, |
|
| 367 | 'Attributes' => [ |
||
| 368 | 2 | 'Policy' => $policy, |
|
| 369 | ] |
||
| 370 | ]); |
||
| 371 | |||
| 372 | 2 | $this->log(200, "Created Updated SQS Policy"); |
|
| 373 | } |
||
| 374 | 3 | } |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Creates a Policy for SQS that's required to allow SNS SendMessage access |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | 2 | public function createSqsPolicy() |
|
| 382 | { |
||
| 383 | 2 | $arn = $this->sqs->getQueueArn($this->queueUrl); |
|
| 384 | |||
| 385 | 2 | return json_encode([ |
|
| 386 | 2 | 'Version' => '2008-10-17', |
|
| 387 | 2 | 'Id' => sprintf('%s/SQSDefaultPolicy', $arn), |
|
| 388 | 'Statement' => [ |
||
| 389 | [ |
||
| 390 | 2 | 'Sid' => 'SNSPermissions', |
|
| 391 | 2 | 'Effect' => 'Allow', |
|
| 392 | 'Principal' => ['AWS' => '*'], |
||
| 393 | 2 | 'Action' => 'SQS:SendMessage', |
|
| 394 | 2 | 'Resource' => $arn |
|
| 395 | ] |
||
| 396 | ] |
||
| 397 | ]); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Checks to see if a Topic exists |
||
| 402 | * |
||
| 403 | * This method relies on in-memory cache and the Cache provider |
||
| 404 | * to reduce the need to needlessly call the create method on an existing |
||
| 405 | * Topic. |
||
| 406 | * |
||
| 407 | * @return boolean |
||
| 408 | */ |
||
| 409 | 3 | public function topicExists() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Creates a SNS Topic and returns the ARN |
||
| 445 | * |
||
| 446 | * The create method for the SNS Topics is idempotent - if the topic already |
||
| 447 | * exists, this method will return the Topic ARN of the existing Topic. |
||
| 448 | * |
||
| 449 | * |
||
| 450 | * @return false|null |
||
| 451 | */ |
||
| 452 | 2 | public function createTopic() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get a list of Subscriptions for the specified SNS Topic |
||
| 472 | * |
||
| 473 | * @param string $topicArn The SNS Topic Arn |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | 4 | public function getTopicSubscriptions($topicArn) |
|
| 478 | { |
||
| 479 | 4 | $result = $this->sns->listSubscriptionsByTopic([ |
|
| 480 | 4 | 'TopicArn' => $topicArn |
|
| 481 | ]); |
||
| 482 | |||
| 483 | 4 | return $result->get('Subscriptions'); |
|
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Subscribes an endpoint to a SNS Topic |
||
| 488 | * |
||
| 489 | * @param string $topicArn The ARN of the Topic |
||
| 490 | * @param string $protocol The protocol of the Endpoint |
||
| 491 | * @param string $endpoint The Endpoint of the Subscriber |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 2 | public function subscribeToTopic($topicArn, $protocol, $endpoint) |
|
| 496 | { |
||
| 497 | // Check against the current Topic Subscriptions |
||
| 498 | 2 | $subscriptions = $this->getTopicSubscriptions($topicArn); |
|
| 499 | 2 | foreach ($subscriptions as $subscription) { |
|
| 500 | 2 | if ($endpoint === $subscription['Endpoint']) { |
|
| 501 | 2 | return $subscription['SubscriptionArn']; |
|
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | 1 | $result = $this->sns->subscribe([ |
|
| 506 | 1 | 'TopicArn' => $topicArn, |
|
| 507 | 1 | 'Protocol' => $protocol, |
|
| 508 | 1 | 'Endpoint' => $endpoint |
|
| 509 | ]); |
||
| 510 | |||
| 511 | 1 | $arn = $result->get('SubscriptionArn'); |
|
| 512 | |||
| 513 | $context = [ |
||
| 514 | 1 | 'Endpoint' => $endpoint, |
|
| 515 | 1 | 'Protocol' => $protocol, |
|
| 516 | 1 | 'SubscriptionArn' => $arn |
|
| 517 | ]; |
||
| 518 | 1 | $this->log(200, "Endpoint Subscribed to SNS Topic", $context); |
|
| 519 | |||
| 520 | 1 | return $arn; |
|
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Unsubscribes an endpoint from a SNS Topic |
||
| 525 | * |
||
| 526 | * The method will return TRUE on success, or FALSE if the Endpoint did not |
||
| 527 | * have a Subscription on the SNS Topic |
||
| 528 | * |
||
| 529 | * @param string $topicArn The ARN of the Topic |
||
| 530 | * @param string $protocol The protocol of the Endpoint |
||
| 531 | * @param string $endpoint The Endpoint of the Subscriber |
||
| 532 | * |
||
| 533 | * @return Boolean |
||
| 534 | */ |
||
| 535 | 1 | public function unsubscribeFromTopic($topicArn, $protocol, $endpoint) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Handles SNS Notifications |
||
| 561 | * |
||
| 562 | * For Subscription notifications, this method will automatically confirm |
||
| 563 | * the Subscription request |
||
| 564 | * |
||
| 565 | * For Message notifications, this method polls the queue and dispatches |
||
| 566 | * the `{queue}.message_received` event for each message retrieved |
||
| 567 | * |
||
| 568 | * @param NotificationEvent $event The Notification Event |
||
| 569 | * @param string $eventName Name of the event |
||
| 570 | * @param EventDispatcherInterface $dispatcher |
||
| 571 | * @return bool|void |
||
| 572 | */ |
||
| 573 | 2 | public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Removes the message from queue after all other listeners have fired |
||
| 600 | * |
||
| 601 | * If an earlier listener has erred or stopped propagation, this method |
||
| 602 | * will not fire and the Queued Message should become visible in queue again. |
||
| 603 | * |
||
| 604 | * Stops Event Propagation after removing the Message |
||
| 605 | * |
||
| 606 | * @param MessageEvent $event The SQS Message Event |
||
| 607 | * @return bool|void |
||
| 608 | */ |
||
| 609 | 1 | public function onMessageReceived(MessageEvent $event) |
|
| 620 | } |
||
| 621 |