PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Sqs/SqsClient.php

https://gitlab.com/github-cloud-corp/aws-sdk-php
PHP | 141 lines | 79 code | 6 blank | 56 comment | 4 complexity | 438fd5b5eb72ea4193f34faa6a8664aa MD5 | raw file
  1. <?php
  2. namespace Aws\Sqs;
  3. use Aws\AwsClient;
  4. use Aws\CommandInterface;
  5. use Aws\Sqs\Exception\SqsException;
  6. use GuzzleHttp\Psr7\Uri;
  7. use Psr\Http\Message\RequestInterface;
  8. /**
  9. * Client used to interact Amazon Simple Queue Service (Amazon SQS)
  10. *
  11. * @method \Aws\Result addPermission(array $args = [])
  12. * @method \GuzzleHttp\Promise\Promise addPermissionAsync(array $args = [])
  13. * @method \Aws\Result changeMessageVisibility(array $args = [])
  14. * @method \GuzzleHttp\Promise\Promise changeMessageVisibilityAsync(array $args = [])
  15. * @method \Aws\Result changeMessageVisibilityBatch(array $args = [])
  16. * @method \GuzzleHttp\Promise\Promise changeMessageVisibilityBatchAsync(array $args = [])
  17. * @method \Aws\Result createQueue(array $args = [])
  18. * @method \GuzzleHttp\Promise\Promise createQueueAsync(array $args = [])
  19. * @method \Aws\Result deleteMessage(array $args = [])
  20. * @method \GuzzleHttp\Promise\Promise deleteMessageAsync(array $args = [])
  21. * @method \Aws\Result deleteMessageBatch(array $args = [])
  22. * @method \GuzzleHttp\Promise\Promise deleteMessageBatchAsync(array $args = [])
  23. * @method \Aws\Result deleteQueue(array $args = [])
  24. * @method \GuzzleHttp\Promise\Promise deleteQueueAsync(array $args = [])
  25. * @method \Aws\Result getQueueAttributes(array $args = [])
  26. * @method \GuzzleHttp\Promise\Promise getQueueAttributesAsync(array $args = [])
  27. * @method \Aws\Result getQueueUrl(array $args = [])
  28. * @method \GuzzleHttp\Promise\Promise getQueueUrlAsync(array $args = [])
  29. * @method \Aws\Result listDeadLetterSourceQueues(array $args = [])
  30. * @method \GuzzleHttp\Promise\Promise listDeadLetterSourceQueuesAsync(array $args = [])
  31. * @method \Aws\Result listQueues(array $args = [])
  32. * @method \GuzzleHttp\Promise\Promise listQueuesAsync(array $args = [])
  33. * @method \Aws\Result purgeQueue(array $args = [])
  34. * @method \GuzzleHttp\Promise\Promise purgeQueueAsync(array $args = [])
  35. * @method \Aws\Result receiveMessage(array $args = [])
  36. * @method \GuzzleHttp\Promise\Promise receiveMessageAsync(array $args = [])
  37. * @method \Aws\Result removePermission(array $args = [])
  38. * @method \GuzzleHttp\Promise\Promise removePermissionAsync(array $args = [])
  39. * @method \Aws\Result sendMessage(array $args = [])
  40. * @method \GuzzleHttp\Promise\Promise sendMessageAsync(array $args = [])
  41. * @method \Aws\Result sendMessageBatch(array $args = [])
  42. * @method \GuzzleHttp\Promise\Promise sendMessageBatchAsync(array $args = [])
  43. * @method \Aws\Result setQueueAttributes(array $args = [])
  44. * @method \GuzzleHttp\Promise\Promise setQueueAttributesAsync(array $args = [])
  45. */
  46. class SqsClient extends AwsClient
  47. {
  48. public function __construct(array $config)
  49. {
  50. parent::__construct($config);
  51. $list = $this->getHandlerList();
  52. $list->appendBuild($this->queueUrl(), 'sqs.queue_url');
  53. $list->appendSign($this->validateMd5(), 'sqs.md5');
  54. }
  55. /**
  56. * Converts a queue URL into a queue ARN.
  57. *
  58. * @param string $queueUrl The queue URL to perform the action on.
  59. * Retrieved when the queue is first created.
  60. *
  61. * @return string An ARN representation of the queue URL.
  62. */
  63. public function getQueueArn($queueUrl)
  64. {
  65. return strtr($queueUrl, array(
  66. 'http://' => 'arn:aws:',
  67. 'https://' => 'arn:aws:',
  68. '.amazonaws.com' => '',
  69. '/' => ':',
  70. '.' => ':',
  71. ));
  72. }
  73. /**
  74. * Moves the URI of the queue to the URI in the input parameter.
  75. *
  76. * @return callable
  77. */
  78. private function queueUrl()
  79. {
  80. return static function (callable $handler) {
  81. return function (
  82. CommandInterface $c,
  83. RequestInterface $r = null
  84. ) use ($handler) {
  85. if ($c->hasParam('QueueUrl')) {
  86. $uri = Uri::resolve($r->getUri(), $c['QueueUrl']);
  87. $r = $r->withUri($uri);
  88. }
  89. return $handler($c, $r);
  90. };
  91. };
  92. }
  93. /**
  94. * Validates ReceiveMessage body MD5s
  95. *
  96. * @return callable
  97. */
  98. private function validateMd5()
  99. {
  100. return static function (callable $handler) {
  101. return function (
  102. CommandInterface $c,
  103. RequestInterface $r = null
  104. ) use ($handler) {
  105. if ($c->getName() !== 'ReceiveMessage') {
  106. return $handler($c, $r);
  107. }
  108. return $handler($c, $r)
  109. ->then(
  110. function ($result) use ($c, $r) {
  111. foreach ((array) $result['Messages'] as $msg) {
  112. if (isset($msg['MD5OfBody'])
  113. && md5($msg['Body']) !== $msg['MD5OfBody']
  114. ) {
  115. throw new SqsException(
  116. sprintf(
  117. 'MD5 mismatch. Expected %s, found %s',
  118. $msg['MD5OfBody'],
  119. md5($msg['Body'])
  120. ),
  121. $c,
  122. [
  123. 'code' => 'ClientChecksumMismatch',
  124. 'request' => $r
  125. ]
  126. );
  127. }
  128. }
  129. return $result;
  130. }
  131. );
  132. };
  133. };
  134. }
  135. }