Message.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Aws\Sns;
  3. /**
  4. * Represents an SNS message received over http(s).
  5. */
  6. class Message implements \ArrayAccess, \IteratorAggregate
  7. {
  8. private static $requiredKeys = [
  9. 'Message',
  10. 'MessageId',
  11. 'Timestamp',
  12. 'TopicArn',
  13. 'Type',
  14. 'Signature',
  15. 'SigningCertURL',
  16. 'SignatureVersion',
  17. ];
  18. /** @var array The message data */
  19. private $data;
  20. /**
  21. * Creates a message object from the raw POST data
  22. *
  23. * @return Message
  24. * @throws \RuntimeException If the POST data is absent, or not a valid JSON document
  25. */
  26. public static function fromRawPostData()
  27. {
  28. // Make sure the SNS-provided header exists.
  29. if (!isset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE'])) {
  30. throw new \RuntimeException('SNS message type header not provided.');
  31. }
  32. // Read the raw POST data and JSON-decode it.
  33. $data = json_decode(file_get_contents('php://input'), true);
  34. if (JSON_ERROR_NONE !== json_last_error() || !is_array($data)) {
  35. throw new \RuntimeException('Invalid POST data.');
  36. }
  37. return new Message($data);
  38. }
  39. /**
  40. * Creates a Message object from an array of raw message data.
  41. *
  42. * @param array $data The message data.
  43. *
  44. * @throws \InvalidArgumentException If a valid type is not provided or
  45. * there are other required keys missing.
  46. */
  47. public function __construct(array $data)
  48. {
  49. // Ensure that all the required keys for the message's type are present.
  50. $this->validateRequiredKeys($data, self::$requiredKeys);
  51. if ($data['Type'] === 'SubscriptionConfirmation'
  52. || $data['Type'] === 'UnsubscribeConfirmation'
  53. ) {
  54. $this->validateRequiredKeys($data, ['SubscribeURL', 'Token']);
  55. }
  56. $this->data = $data;
  57. }
  58. public function getIterator()
  59. {
  60. return new \ArrayIterator($this->data);
  61. }
  62. public function offsetExists($key)
  63. {
  64. return isset($this->data[$key]);
  65. }
  66. public function offsetGet($key)
  67. {
  68. return isset($this->data[$key]) ? $this->data[$key] : null;
  69. }
  70. public function offsetSet($key, $value)
  71. {
  72. $this->data[$key] = $value;
  73. }
  74. public function offsetUnset($key)
  75. {
  76. unset($this->data[$key]);
  77. }
  78. /**
  79. * Get all the message data as a plain array.
  80. *
  81. * @return array
  82. */
  83. public function toArray()
  84. {
  85. return $this->data;
  86. }
  87. private function validateRequiredKeys(array $data, array $keys)
  88. {
  89. foreach ($keys as $key) {
  90. if (!isset($data[$key])) {
  91. throw new \InvalidArgumentException(
  92. "\"{$key}\" is required to verify the SNS Message."
  93. );
  94. }
  95. }
  96. }
  97. }