|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BabelQueue\Transport; |
| 6 | + |
| 7 | +use BabelQueue\Codec\EnvelopeCodec; |
| 8 | +use BabelQueue\Contracts\Transport; |
| 9 | + |
| 10 | +/** |
| 11 | + * A framework-less Amazon SQS producer: sends the canonical envelope as the message |
| 12 | + * body and projects the envelope onto the native SQS MessageAttributes every |
| 13 | + * BabelQueue SDK agrees on, so a non-PHP consumer can route on `bq-job` and trace on |
| 14 | + * `bq-trace-id` *without decoding the body first*: |
| 15 | + * |
| 16 | + * - `bq-job` ← the job URN |
| 17 | + * - `bq-trace-id` ← trace_id |
| 18 | + * - `bq-message-id` ← meta.id |
| 19 | + * - `bq-schema-version` / `bq-source-lang` / `bq-created-at` |
| 20 | + * |
| 21 | + * Implements §3 of the broker-bindings contract. The envelope is unchanged |
| 22 | + * (`schema_version` stays 1); SQS is purely additive. |
| 23 | + * |
| 24 | + * Optional dependency: `aws/aws-sdk-php`. Wrap your client in one line: |
| 25 | + * |
| 26 | + * ```php |
| 27 | + * $sqs = new \Aws\Sqs\SqsClient([...]); |
| 28 | + * $transport = new SqsTransport( |
| 29 | + * new class ($sqs) implements SqsClient { |
| 30 | + * public function __construct(private \Aws\Sqs\SqsClient $c) {} |
| 31 | + * public function sendMessage(array $args): mixed { return $this->c->sendMessage($args); } |
| 32 | + * }, |
| 33 | + * 'https://sqs.eu-central-1.amazonaws.com/123456789012/orders', |
| 34 | + * ); |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +final class SqsTransport implements Transport |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private readonly SqsClient $client, |
| 41 | + private readonly string $queueUrl, |
| 42 | + private readonly bool $fifo = false, |
| 43 | + private readonly ?string $messageGroupId = null, |
| 44 | + private readonly bool $contentDedup = false, |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + public function publish(string $payload, ?string $queue = null): ?string |
| 49 | + { |
| 50 | + $url = $queue ?? $this->queueUrl; |
| 51 | + $envelope = EnvelopeCodec::decode($payload); |
| 52 | + $meta = is_array($envelope['meta'] ?? null) ? $envelope['meta'] : []; |
| 53 | + |
| 54 | + $args = [ |
| 55 | + 'QueueUrl' => $url, |
| 56 | + 'MessageBody' => $payload, |
| 57 | + 'MessageAttributes' => $this->attributes($envelope, $meta), |
| 58 | + ]; |
| 59 | + |
| 60 | + if ($this->fifo) { |
| 61 | + $args['MessageGroupId'] = $this->messageGroupId ?? $this->queueName($url); |
| 62 | + if (! $this->contentDedup && isset($meta['id']) && is_scalar($meta['id'])) { |
| 63 | + $args['MessageDeduplicationId'] = (string) $meta['id']; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $this->client->sendMessage($args); |
| 68 | + |
| 69 | + $id = $meta['id'] ?? null; |
| 70 | + |
| 71 | + return is_scalar($id) ? (string) $id : null; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param array<string, mixed> $envelope |
| 76 | + * @param array<string, mixed> $meta |
| 77 | + * @return array<string, array{DataType: string, StringValue: string}> |
| 78 | + */ |
| 79 | + private function attributes(array $envelope, array $meta): array |
| 80 | + { |
| 81 | + $attributes = []; |
| 82 | + |
| 83 | + $urn = EnvelopeCodec::urn($envelope); |
| 84 | + if ($urn !== '') { |
| 85 | + $attributes['bq-job'] = self::string($urn); |
| 86 | + } |
| 87 | + |
| 88 | + $traceId = $envelope['trace_id'] ?? null; |
| 89 | + if (is_string($traceId) && $traceId !== '') { |
| 90 | + $attributes['bq-trace-id'] = self::string($traceId); |
| 91 | + } |
| 92 | + |
| 93 | + if (isset($meta['id']) && is_scalar($meta['id'])) { |
| 94 | + $attributes['bq-message-id'] = self::string((string) $meta['id']); |
| 95 | + } |
| 96 | + if (isset($meta['schema_version']) && is_scalar($meta['schema_version'])) { |
| 97 | + $attributes['bq-schema-version'] = self::number((string) $meta['schema_version']); |
| 98 | + } |
| 99 | + if (isset($meta['lang']) && is_string($meta['lang']) && $meta['lang'] !== '') { |
| 100 | + $attributes['bq-source-lang'] = self::string($meta['lang']); |
| 101 | + } |
| 102 | + if (isset($meta['created_at']) && is_scalar($meta['created_at'])) { |
| 103 | + $attributes['bq-created-at'] = self::number((string) $meta['created_at']); |
| 104 | + } |
| 105 | + |
| 106 | + return $attributes; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return array{DataType: string, StringValue: string} |
| 111 | + */ |
| 112 | + private static function string(string $value): array |
| 113 | + { |
| 114 | + return ['DataType' => 'String', 'StringValue' => $value]; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return array{DataType: string, StringValue: string} |
| 119 | + */ |
| 120 | + private static function number(string $value): array |
| 121 | + { |
| 122 | + return ['DataType' => 'Number', 'StringValue' => $value]; |
| 123 | + } |
| 124 | + |
| 125 | + private function queueName(string $url): string |
| 126 | + { |
| 127 | + $segments = array_values(array_filter(explode('/', $url), static fn (string $s): bool => $s !== '')); |
| 128 | + |
| 129 | + return $segments === [] ? 'default' : (string) end($segments); |
| 130 | + } |
| 131 | +} |
0 commit comments