LambdaFunctionConfiguration.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace AsyncAws\S3\ValueObject;
  3. use AsyncAws\Core\Exception\InvalidArgument;
  4. use AsyncAws\S3\Enum\Event;
  5. /**
  6. * A container for specifying the configuration for Lambda notifications.
  7. */
  8. final class LambdaFunctionConfiguration
  9. {
  10. private $id;
  11. /**
  12. * The Amazon Resource Name (ARN) of the Lambda function that Amazon S3 invokes when the specified event type occurs.
  13. */
  14. private $lambdaFunctionArn;
  15. /**
  16. * The Amazon S3 bucket event for which to invoke the Lambda function. For more information, see Supported Event Types
  17. * [^1] in the *Amazon S3 User Guide*.
  18. *
  19. * [^1]: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
  20. */
  21. private $events;
  22. private $filter;
  23. /**
  24. * @param array{
  25. * Id?: null|string,
  26. * LambdaFunctionArn: string,
  27. * Events: list<Event::*>,
  28. * Filter?: null|NotificationConfigurationFilter|array,
  29. * } $input
  30. */
  31. public function __construct(array $input)
  32. {
  33. $this->id = $input['Id'] ?? null;
  34. $this->lambdaFunctionArn = $input['LambdaFunctionArn'] ?? null;
  35. $this->events = $input['Events'] ?? null;
  36. $this->filter = isset($input['Filter']) ? NotificationConfigurationFilter::create($input['Filter']) : null;
  37. }
  38. public static function create($input): self
  39. {
  40. return $input instanceof self ? $input : new self($input);
  41. }
  42. /**
  43. * @return list<Event::*>
  44. */
  45. public function getEvents(): array
  46. {
  47. return $this->events ?? [];
  48. }
  49. public function getFilter(): ?NotificationConfigurationFilter
  50. {
  51. return $this->filter;
  52. }
  53. public function getId(): ?string
  54. {
  55. return $this->id;
  56. }
  57. public function getLambdaFunctionArn(): string
  58. {
  59. return $this->lambdaFunctionArn;
  60. }
  61. /**
  62. * @internal
  63. */
  64. public function requestBody(\DOMElement $node, \DOMDocument $document): void
  65. {
  66. if (null !== $v = $this->id) {
  67. $node->appendChild($document->createElement('Id', $v));
  68. }
  69. if (null === $v = $this->lambdaFunctionArn) {
  70. throw new InvalidArgument(sprintf('Missing parameter "LambdaFunctionArn" for "%s". The value cannot be null.', __CLASS__));
  71. }
  72. $node->appendChild($document->createElement('CloudFunction', $v));
  73. if (null === $v = $this->events) {
  74. throw new InvalidArgument(sprintf('Missing parameter "Events" for "%s". The value cannot be null.', __CLASS__));
  75. }
  76. foreach ($v as $item) {
  77. if (!Event::exists($item)) {
  78. throw new InvalidArgument(sprintf('Invalid parameter "Event" for "%s". The value "%s" is not a valid "Event".', __CLASS__, $item));
  79. }
  80. $node->appendChild($document->createElement('Event', $item));
  81. }
  82. if (null !== $v = $this->filter) {
  83. $node->appendChild($child = $document->createElement('Filter'));
  84. $v->requestBody($child, $document);
  85. }
  86. }
  87. }