diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f08f1443..e446d49c2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,24 @@
# Change Log
+## [0.9.8](https://github.com/php-enqueue/enqueue-dev/tree/0.9.8) (2019-02-27)
+[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.7...0.9.8)
+
+**Merged pull requests:**
+
+- Add upgrade instructions [\#787](https://github.com/php-enqueue/enqueue-dev/pull/787) ([KDederichs](https://github.com/KDederichs))
+- \[consumption\] Fix exception loop in QueueConsumer [\#776](https://github.com/php-enqueue/enqueue-dev/pull/776) ([enumag](https://github.com/enumag))
+- \[consumption\] Add ability to change process exit status from within queue consumer extension [\#766](https://github.com/php-enqueue/enqueue-dev/pull/766) ([greblov](https://github.com/greblov))
+- \[amqp-tools\] Fix amqp-tools dependency [\#785](https://github.com/php-enqueue/enqueue-dev/pull/785) ([TomPradat](https://github.com/TomPradat))
+- \[amqp-tools\] Enable 'ssl\_on' param for 'ssl' scheme extension [\#781](https://github.com/php-enqueue/enqueue-dev/pull/781) ([Leprechaunz](https://github.com/Leprechaunz))
+- \[amqp-bunny\] Catch signal in Bunny adapter [\#771](https://github.com/php-enqueue/enqueue-dev/pull/771) ([snapshotpl](https://github.com/snapshotpl))
+- \[amqp-lib\] supporting channel\_rpc\_timeout option [\#755](https://github.com/php-enqueue/enqueue-dev/pull/755) ([derek9gag](https://github.com/derek9gag))
+- \[dbal\]: make dbal connection config usable again [\#765](https://github.com/php-enqueue/enqueue-dev/pull/765) ([ssiergl](https://github.com/ssiergl))
+- \[fs\] polling\_interval config should be milliseconds not microseconds [\#764](https://github.com/php-enqueue/enqueue-dev/pull/764) ([ssiergl](https://github.com/ssiergl))
+- \[simple-client\] Fix Logger Initialisation [\#752](https://github.com/php-enqueue/enqueue-dev/pull/752) ([ajbonner](https://github.com/ajbonner))
+- \[snsqs\] Corrected the installation part in the docs/transport/snsqs.md [\#791](https://github.com/php-enqueue/enqueue-dev/pull/791) ([dgreda](https://github.com/dgreda))
+- \[sqs\] Update SqsConnectionFactory.php [\#751](https://github.com/php-enqueue/enqueue-dev/pull/751) ([Orkin](https://github.com/Orkin))
+- correct typo in composer.json [\#767](https://github.com/php-enqueue/enqueue-dev/pull/767) ([greblov](https://github.com/greblov))
+
## [0.9.7](https://github.com/php-enqueue/enqueue-dev/tree/0.9.7) (2019-02-01)
[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.6...0.9.7)
diff --git a/README.md b/README.md
index e08bdfcf9..df5b154aa 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,14 @@ Features:
[](https://travis-ci.org/php-enqueue/sqs)
[](https://packagist.org/packages/enqueue/sqs/stats)
[](https://packagist.org/packages/enqueue/sqs)
+ * [Amazon SNS](docs/transport/sns.md)
+[](https://travis-ci.org/php-enqueue/sns)
+[](https://packagist.org/packages/enqueue/sns/stats)
+[](https://packagist.org/packages/enqueue/sns)
+ * [Amazon SNS\SQS](docs/transport/snsqs.md)
+[](https://travis-ci.org/php-enqueue/snsqs)
+[](https://packagist.org/packages/enqueue/snsqs/stats)
+[](https://packagist.org/packages/enqueue/snsqs)
* [Google PubSub](docs/transport/gps.md)
[](https://travis-ci.org/php-enqueue/gps)
[](https://packagist.org/packages/enqueue/gps/stats)
diff --git a/UPGRADE.md b/UPGRADE.md
new file mode 100644
index 000000000..6b64e44d9
--- /dev/null
+++ b/UPGRADE.md
@@ -0,0 +1,89 @@
+# Upgrading Enqueue:
+
+From `0.8.x` to `0.9.x`:
+
+## Processor declaration
+
+`Interop\Queue\PsrProcessor` interface has been replaced by `Interop\Queue\Processor`
+`Interop\Queue\PsrMessage` interface has been replaced by `Interop\Queue\Message`
+`Interop\Queue\PsrContext` interface has been replaced by `Interop\Queue\Context`
+
+
+
+## Symfony Bundle
+
+### Configuration changes:
+
+`0.8.x`
+
+
+```
+enqueue:
+ transport:
+ default: ...
+```
+
+`0.9.x`
+
+
+```
+enqueue:
+ default:
+ transport: ...
+```
+
+In `0.9.x` the client name is a root config node.
+
+The `default_processor_queue` Client option was removed.
+
+### Service declarations:
+
+`0.8.x`
+
+
+```
+tags:
+ - { name: 'enqueue.client.processor' }
+```
+
+`0.9.x`
+
+
+```
+tags:
+ - { name: 'enqueue.command_subscriber' }
+ - { name: 'enqueue.topic_subscriber' }
+ - { name: 'enqueue.processor' }
+```
+
+The tag to register message processors has changed and is now split into processor sub types.
+
+### CommandSubscriberInterface `getSubscribedCommand`
+
+
+`0.8.x`
+
+return `aCommandName` or
+```
+ [
+ 'processorName' => 'aCommandName',
+ 'queueName' => 'a_client_queue_name',
+ 'queueNameHardcoded' => true,
+ 'exclusive' => true,
+ ]
+```
+
+`0.9.x`
+
+
+return `aCommandName` or
+```
+ [
+ 'command' => 'aSubscribedCommand',
+ 'processor' => 'aProcessorName',
+ 'queue' => 'a_client_queue_name',
+ 'prefix_queue' => true,
+ 'exclusive' => true,
+ ]
+```
+
diff --git a/bin/subtree-split b/bin/subtree-split
index c52f15a8e..e8aa1ed4c 100755
--- a/bin/subtree-split
+++ b/bin/subtree-split
@@ -58,6 +58,8 @@ remote rdkafka git@github.com:php-enqueue/rdkafka.git
remote dbal git@github.com:php-enqueue/dbal.git
remote null git@github.com:php-enqueue/null.git
remote sqs git@github.com:php-enqueue/sqs.git
+remote sns git@github.com:php-enqueue/sns.git
+remote snsqs git@github.com:php-enqueue/snsqs.git
remote gps git@github.com:php-enqueue/gps.git
remote enqueue-bundle git@github.com:php-enqueue/enqueue-bundle.git
remote job-queue git@github.com:php-enqueue/job-queue.git
@@ -84,6 +86,8 @@ split 'pkg/redis' redis
split 'pkg/dbal' dbal
split 'pkg/null' null
split 'pkg/sqs' sqs
+split 'pkg/sns' sns
+split 'pkg/snsqs' snsqs
split 'pkg/gps' gps
split 'pkg/enqueue-bundle' enqueue-bundle
split 'pkg/job-queue' job-queue
diff --git a/composer.json b/composer.json
index f8ef90bcc..639b274b6 100644
--- a/composer.json
+++ b/composer.json
@@ -25,7 +25,7 @@
"doctrine/orm": "~2.4",
"mongodb/mongodb": "^1.2",
"pda/pheanstalk": "^3",
- "aws/aws-sdk-php": "~3.26",
+ "aws/aws-sdk-php": "^3.26",
"stomp-php/stomp-php": "^4",
"php-http/guzzle6-adapter": "^1.1",
"php-http/client-common": "^1.7@dev",
@@ -76,6 +76,8 @@
"Enqueue\\Redis\\": "pkg/redis/",
"Enqueue\\SimpleClient\\": "pkg/simple-client/",
"Enqueue\\Sqs\\": "pkg/sqs/",
+ "Enqueue\\Sns\\": "pkg/sns/",
+ "Enqueue\\SnsQs\\": "pkg/snsqs/",
"Enqueue\\Stomp\\": "pkg/stomp/",
"Enqueue\\Test\\": "pkg/test/",
"Enqueue\\Dsn\\": "pkg/dsn/",
diff --git a/docker-compose.yml b/docker-compose.yml
index d302118ad..87260d4e0 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -34,6 +34,8 @@ services:
- PHPREDIS_DSN=redis+phpredis://redis
- GPS_DSN=gps:?projectId=mqdev&emulatorHost=http://google-pubsub:8085
- SQS_DSN=sqs:?key=key&secret=secret®ion=us-east-1&endpoint=http://localstack:4576&version=latest
+ - SNS_DSN=sns:?key=key&secret=secret®ion=us-east-1&endpoint=http://localstack:4575&version=latest
+ - SNSQS_DSN=snsqs:?key=key&secret=secret®ion=us-east-1&sns_endpoint=http://localstack:4575&sqs_endpoint=http://localstack:4576&version=latest
- WAMP_DSN=wamp://thruway:9090
- REDIS_HOST=redis
- REDIS_PORT=6379
@@ -129,9 +131,10 @@ services:
image: 'localstack/localstack:latest'
ports:
- '4576:4576'
+ - '4575:4575'
environment:
HOSTNAME_EXTERNAL: 'localstack'
- SERVICES: 'sqs'
+ SERVICES: 'sqs,sns'
influxdb:
image: 'influxdb:latest'
diff --git a/docs/consumption/extensions.md b/docs/consumption/extensions.md
index aa09b5701..ff59d09b6 100644
--- a/docs/consumption/extensions.md
+++ b/docs/consumption/extensions.md
@@ -59,4 +59,8 @@ The extension catch process signals and gracefully stops consumption. Works only
The extension checks whether the received message is redelivered (There was attempt to process message but it failed).
If so the extension reject the origin message and creates a copy message with a delay.
+## [ConsumerMonitoringExtension](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/monitoring.md#consumption-extension)
+
+There is an extension ConsumerMonitoringExtension for Enqueue QueueConsumer. It could collect consumed messages and consumer stats for you and send them to Grafana, InfluxDB or Datadog.
+
[back to index](../index.md)
diff --git a/docs/index.md b/docs/index.md
index c369d6b9f..521e642fa 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -12,6 +12,7 @@ Enqueue is an MIT-licensed open source project with its ongoing development made
* [Quick tour](quick_tour.md)
* [Transports](#transports)
- Amqp based on [the ext](transport/amqp.md), [bunny](transport/amqp_bunny.md), [the lib](transport/amqp_lib.md)
+ - [Amazon SNS-SQS](transport/snsqs.md)
- [Amazon SQS](transport/sqs.md)
- [Google PubSub](transport/gps.md)
- [Beanstalk (Pheanstalk)](transport/pheanstalk.md)
diff --git a/docs/transport/snsqs.md b/docs/transport/snsqs.md
new file mode 100644
index 000000000..4f5b31ae5
--- /dev/null
+++ b/docs/transport/snsqs.md
@@ -0,0 +1,182 @@
+
Supporting Enqueue
+
+Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider:
+
+- [Become a sponsor](https://www.patreon.com/makasim)
+- [Become our client](http://forma-pro.com/)
+
+---
+
+# Amazon SNS-SQS transport
+
+Utilize two Amazon services [SNS-SQS](https://docs.aws.amazon.com/sns/latest/dg/sns-sqs-as-subscriber.html) to
+implement [Publish-Subscribe](https://www.enterpriseintegrationpatterns.com/patterns/messaging/PublishSubscribeChannel.html)
+enterprise integration pattern. As opposed to single SQS transport this adds ability to use [MessageBus](https://www.enterpriseintegrationpatterns.com/patterns/messaging/MessageBus.html)
+with enqueue.
+
+A transport for [Amazon SQS](https://aws.amazon.com/sqs/) broker.
+It uses internally official [aws sdk library](https://packagist.org/packages/aws/aws-sdk-php)
+
+* [Installation](#installation)
+* [Create context](#create-context)
+* [Declare topic, queue and bind them together](#declare-topic-queue-and-bind-them-together)
+* [Send message to topic](#send-message-to-topic)
+* [Send message to queue](#send-message-to-queue)
+* [Consume message](#consume-message)
+* [Purge queue messages](#purge-queue-messages)
+* [Queue from another AWS account](#queue-from-another-aws-account)
+
+## Installation
+
+```bash
+$ composer require enqueue/snsqs
+```
+
+## Create context
+
+```php
+ 'aKey',
+ 'secret' => 'aSecret',
+ 'region' => 'aRegion',
+
+ // or you can segregate options using prefixes "sns_", "sqs_"
+ 'key' => 'aKey', // common option for both SNS and SQS
+ 'sns_region' => 'aSnsRegion', // SNS transport option
+ 'sqs_region' => 'aSqsRegion', // SQS transport option
+]);
+
+// same as above but given as DSN string. You may need to url encode secret if it contains special char (like +)
+$factory = new SnsQsConnectionFactory('snsqs:?key=aKey&secret=aSecret®ion=aRegion');
+
+$context = $factory->createContext();
+
+// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
+$context = (new \Enqueue\ConnectionFactoryFactory())->create('snsqs:')->createContext();
+```
+
+## Declare topic, queue and bind them together
+
+Declare topic, queue operation creates a topic, queue on a broker side.
+Bind creates connection between topic and queue. You publish message to
+the topic and topic sends message to each queue connected to the topic.
+
+
+```php
+createTopic('in');
+$context->declareTopic($inTopic);
+
+$out1Queue = $context->createQueue('out1');
+$context->declareQueue($out1Queue);
+
+$out2Queue = $context->createQueue('out2');
+$context->declareQueue($out2Queue);
+
+$context->bind($inTopic, $out1Queue);
+$context->bind($inTopic, $out2Queue);
+
+// to remove topic/queue use deleteTopic/deleteQueue method
+//$context->deleteTopic($inTopic);
+//$context->deleteQueue($out1Queue);
+//$context->unbind(inTopic, $out1Queue);
+```
+
+## Send message to topic
+
+```php
+createTopic('in');
+$message = $context->createMessage('Hello world!');
+
+$context->createProducer()->send($inTopic, $message);
+```
+
+## Send message to queue
+
+You can bypass topic and publish message directly to the queue
+
+```php
+createQueue('foo');
+$message = $context->createMessage('Hello world!');
+
+$context->createProducer()->send($fooQueue, $message);
+```
+
+
+## Consume message:
+
+```php
+createQueue('out1');
+$consumer = $context->createConsumer($out1Queue);
+
+$message = $consumer->receive();
+
+// process a message
+
+$consumer->acknowledge($message);
+// $consumer->reject($message);
+```
+
+## Purge queue messages:
+
+```php
+createQueue('foo');
+
+$context->purgeQueue($fooQueue);
+```
+
+## Queue from another AWS account
+
+SQS allows to use queues from another account. You could set it globally for all queues via option `queue_owner_aws_account_id` or
+per queue using `SnsQsQueue::setQueueOwnerAWSAccountId` method.
+
+```php
+createContext();
+
+// per queue.
+$queue = $context->createQueue('foo');
+$queue->setQueueOwnerAWSAccountId('awsAccountId');
+```
+
+## Multi region examples
+
+Enqueue SNSQS provides a generic multi-region support. This enables users to specify which AWS Region to send a command to by setting region on SnsQsQueue.
+If not specified the default region is used.
+
+```php
+createContext();
+
+$queue = $context->createQueue('foo');
+$queue->setRegion('us-west-2');
+
+// the request goes to US West (Oregon) Region
+$context->declareQueue($queue);
+```
+
+[back to index](../index.md)
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 91a1ef407..34885a3d5 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -61,6 +61,14 @@
pkg/sqs/Tests
+
+ pkg/sns/Tests
+
+
+
+ pkg/snsqs/Tests
+
+
pkg/pheanstalk/Tests
diff --git a/pkg/amqp-bunny/AmqpSubscriptionConsumer.php b/pkg/amqp-bunny/AmqpSubscriptionConsumer.php
index 6d7a49469..4fff4f8ab 100644
--- a/pkg/amqp-bunny/AmqpSubscriptionConsumer.php
+++ b/pkg/amqp-bunny/AmqpSubscriptionConsumer.php
@@ -47,7 +47,7 @@ public function consume(int $timeout = 0): void
try {
$this->context->getBunnyChannel()->getClient()->run(0 !== $timeout ? $timeout / 1000 : null);
} catch (ClientException $e) {
- if ('stream_select() failed.' == $e->getMessage() && $signalHandler->wasThereSignal()) {
+ if (0 === strpos($e->getMessage(), 'stream_select() failed') && $signalHandler->wasThereSignal()) {
return;
}
diff --git a/pkg/amqp-lib/AmqpConnectionFactory.php b/pkg/amqp-lib/AmqpConnectionFactory.php
index 169671d12..3cb1c29b7 100644
--- a/pkg/amqp-lib/AmqpConnectionFactory.php
+++ b/pkg/amqp-lib/AmqpConnectionFactory.php
@@ -47,6 +47,7 @@ public function __construct($config = 'amqp:')
->addDefaultOption('login_response', null)
->addDefaultOption('locale', 'en_US')
->addDefaultOption('keepalive', false)
+ ->addDefaultOption('channel_rpc_timeout', 0.)
->addDefaultOption('heartbeat_on_tick', true)
->parse()
;
@@ -120,7 +121,8 @@ private function establishConnection(): AbstractConnection
(int) round(min($this->config->getReadTimeout(), $this->config->getWriteTimeout())),
null,
$this->config->getOption('keepalive'),
- (int) round($this->config->getHeartbeat())
+ (int) round($this->config->getHeartbeat()),
+ $this->config->getOption('channel_rpc_timeout')
);
} else {
$con = new AMQPStreamConnection(
@@ -137,7 +139,8 @@ private function establishConnection(): AbstractConnection
(int) round(min($this->config->getReadTimeout(), $this->config->getWriteTimeout())),
null,
$this->config->getOption('keepalive'),
- (int) round($this->config->getHeartbeat())
+ (int) round($this->config->getHeartbeat()),
+ $this->config->getOption('channel_rpc_timeout')
);
}
} else {
@@ -159,7 +162,8 @@ private function establishConnection(): AbstractConnection
(int) round($this->config->getReadTimeout()),
$this->config->getOption('keepalive'),
(int) round($this->config->getWriteTimeout()),
- (int) round($this->config->getHeartbeat())
+ (int) round($this->config->getHeartbeat()),
+ $this->config->getOption('channel_rpc_timeout')
);
} else {
$con = new AMQPSocketConnection(
@@ -175,7 +179,8 @@ private function establishConnection(): AbstractConnection
(int) round($this->config->getReadTimeout()),
$this->config->getOption('keepalive'),
(int) round($this->config->getWriteTimeout()),
- (int) round($this->config->getHeartbeat())
+ (int) round($this->config->getHeartbeat()),
+ $this->config->getOption('channel_rpc_timeout')
);
}
}
diff --git a/pkg/amqp-tools/ConnectionConfig.php b/pkg/amqp-tools/ConnectionConfig.php
index 5d52891e9..224d54b9d 100644
--- a/pkg/amqp-tools/ConnectionConfig.php
+++ b/pkg/amqp-tools/ConnectionConfig.php
@@ -49,7 +49,7 @@ class ConnectionConfig
private $config;
/**
- * @var array|null|string
+ * @var array|string|null
*/
private $inputConfig;
@@ -391,7 +391,10 @@ private function parseDsn($dsn)
}
$sslOn = false;
- if ('amqps' === $dsn->getSchemeProtocol() || in_array('tls', $dsn->getSchemeExtensions(), true)) {
+ $isAmqps = 'amqps' === $dsn->getSchemeProtocol();
+ $isTls = in_array('tls', $dsn->getSchemeExtensions(), true);
+ $isSsl = in_array('ssl', $dsn->getSchemeExtensions(), true);
+ if ($isAmqps || $isTls || $isSsl) {
$sslOn = true;
}
diff --git a/pkg/amqp-tools/Tests/ConnectionConfigTest.php b/pkg/amqp-tools/Tests/ConnectionConfigTest.php
index d45b847b3..67a3f624b 100644
--- a/pkg/amqp-tools/Tests/ConnectionConfigTest.php
+++ b/pkg/amqp-tools/Tests/ConnectionConfigTest.php
@@ -266,6 +266,32 @@ public static function provideConfigs()
],
];
+ yield [
+ 'amqp+ssl:',
+ [
+ 'host' => 'localhost',
+ 'port' => 5672,
+ 'vhost' => '/',
+ 'user' => 'guest',
+ 'pass' => 'guest',
+ 'read_timeout' => 3.,
+ 'write_timeout' => 3.,
+ 'connection_timeout' => 3.,
+ 'persisted' => false,
+ 'lazy' => true,
+ 'qos_prefetch_size' => 0,
+ 'qos_prefetch_count' => 1,
+ 'qos_global' => false,
+ 'heartbeat' => 0.0,
+ 'ssl_on' => true,
+ 'ssl_verify' => true,
+ 'ssl_cacert' => '',
+ 'ssl_cert' => '',
+ 'ssl_key' => '',
+ 'ssl_passphrase' => '',
+ ],
+ ];
+
yield [
'amqp://user:pass@host:10000/vhost',
[
diff --git a/pkg/amqp-tools/composer.json b/pkg/amqp-tools/composer.json
index aba5b5af3..ef2ae274d 100644
--- a/pkg/amqp-tools/composer.json
+++ b/pkg/amqp-tools/composer.json
@@ -8,13 +8,13 @@
"require": {
"php": "^7.1.3",
"queue-interop/amqp-interop": "^0.8",
- "queue-interop/queue-interop": "^0.7|^0.8"
+ "queue-interop/queue-interop": "^0.7|^0.8",
+ "enqueue/dsn": "^0.9.2"
},
"require-dev": {
"phpunit/phpunit": "~5.4.0",
"enqueue/test": "0.9.x-dev",
- "enqueue/null": "0.9.x-dev",
- "enqueue/dsn": "0.9.x-dev"
+ "enqueue/null": "0.9.x-dev"
},
"support": {
"email": "opensource@forma-pro.com",
diff --git a/pkg/dbal/DbalConnectionFactory.php b/pkg/dbal/DbalConnectionFactory.php
index 7e0f65c45..077bd0e3c 100644
--- a/pkg/dbal/DbalConnectionFactory.php
+++ b/pkg/dbal/DbalConnectionFactory.php
@@ -45,8 +45,7 @@ public function __construct($config = 'mysql:')
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
if (array_key_exists('dsn', $config)) {
- $config = array_replace_recursive($config, $this->parseDsn($config['dsn']));
-
+ $config = array_replace_recursive($config, $this->parseDsn($config['dsn'], $config));
unset($config['dsn']);
}
} else {
@@ -92,7 +91,13 @@ private function establishConnection(): Connection
return $this->connection;
}
- private function parseDsn(string $dsn): array
+ /**
+ * @param string $dsn
+ * @param array|null $config
+ *
+ * @return array
+ */
+ private function parseDsn(string $dsn, array $config = null): array
{
if (false === strpos($dsn, ':')) {
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
@@ -135,6 +140,21 @@ private function parseDsn(string $dsn): array
$doctrineScheme = $supported[$scheme];
+ if ($scheme.':' === $dsn && is_array($config) && array_key_exists('connection', $config)) {
+ $default = [
+ 'driver' => $doctrineScheme,
+ 'host' => 'localhost',
+ 'port' => '3306',
+ 'user' => 'root',
+ 'password' => '',
+ ];
+
+ return [
+ 'lazy' => true,
+ 'connection' => array_replace_recursive($default, $config['connection']),
+ ];
+ }
+
return [
'lazy' => true,
'connection' => [
diff --git a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php
index 1b9d8634c..fb5cae5d5 100644
--- a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php
+++ b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php
@@ -101,6 +101,72 @@ public static function provideConfigs()
],
];
+ yield [
+ [
+ 'dsn' => 'mysql+pdo:',
+ 'connection' => [
+ 'dbname' => 'customDbName',
+ ],
+ ],
+ [
+ 'connection' => [
+ 'dbname' => 'customDbName',
+ 'driver' => 'pdo_mysql',
+ 'host' => 'localhost',
+ 'port' => '3306',
+ 'user' => 'root',
+ 'password' => '',
+ ],
+ 'table_name' => 'enqueue',
+ 'polling_interval' => 1000,
+ 'lazy' => true,
+ ],
+ ];
+
+ yield [
+ [
+ 'dsn' => 'mysql+pdo:',
+ 'connection' => [
+ 'dbname' => 'customDbName',
+ 'host' => 'host',
+ 'port' => '10000',
+ 'user' => 'user',
+ 'password' => 'pass',
+ ],
+ ],
+ [
+ 'connection' => [
+ 'dbname' => 'customDbName',
+ 'host' => 'host',
+ 'port' => '10000',
+ 'user' => 'user',
+ 'password' => 'pass',
+ 'driver' => 'pdo_mysql',
+ ],
+ 'table_name' => 'enqueue',
+ 'polling_interval' => 1000,
+ 'lazy' => true,
+ ],
+ ];
+
+ yield [
+ [
+ 'dsn' => 'mysql+pdo://user:pass@host:10000/db',
+ 'connection' => [
+ 'foo' => 'fooValue',
+ ],
+ ],
+ [
+ 'connection' => [
+ 'foo' => 'fooValue',
+ 'url' => 'pdo_mysql://user:pass@host:10000/db',
+ ],
+ 'table_name' => 'enqueue',
+ 'polling_interval' => 1000,
+ 'lazy' => true,
+ ],
+ ];
+
yield [
'mysql://user:pass@host:10000/db',
[
diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
index ee464080f..0a5f38325 100644
--- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
+++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
@@ -143,6 +143,14 @@ public function provideEnqueueConfigs()
],
]];
+ yield 'snsqs' => [[
+ 'default' => [
+ 'transport' => [
+ 'dsn' => getenv('SNSQS_DSN'),
+ ],
+ ],
+ ]];
+
//
// yield 'gps' => [[
// 'transport' => [
diff --git a/pkg/enqueue/Client/Driver/SnsQsDriver.php b/pkg/enqueue/Client/Driver/SnsQsDriver.php
new file mode 100644
index 000000000..2b1d4f233
--- /dev/null
+++ b/pkg/enqueue/Client/Driver/SnsQsDriver.php
@@ -0,0 +1,90 @@
+debug(sprintf('[SqsQsDriver] '.$text, ...$args));
+ };
+
+ // setup router
+ $routerTopic = $this->createRouterTopic();
+ $log('Declare router topic: %s', $routerTopic->getTopicName());
+ $this->getContext()->declareTopic($routerTopic);
+
+ $routerQueue = $this->createQueue($this->getConfig()->getRouterQueue());
+ $log('Declare router queue: %s', $routerQueue->getQueueName());
+ $this->getContext()->declareQueue($routerQueue);
+
+ $log('Bind router queue to topic: %s -> %s', $routerQueue->getQueueName(), $routerTopic->getTopicName());
+ $this->getContext()->bind($routerTopic, $routerQueue);
+
+ // setup queues
+ $declaredQueues = [];
+ $declaredTopics = [];
+ foreach ($this->getRouteCollection()->all() as $route) {
+ $queue = $this->createRouteQueue($route);
+ if (false === array_key_exists($queue->getQueueName(), $declaredQueues)) {
+ $log('Declare processor queue: %s', $queue->getQueueName());
+ $this->getContext()->declareQueue($queue);
+
+ $declaredQueues[$queue->getQueueName()] = true;
+ }
+
+ if ($route->isCommand()) {
+ continue;
+ }
+
+ $topic = $this->doCreateTopic($this->createTransportQueueName($route->getSource(), true));
+ if (false === array_key_exists($topic->getTopicName(), $declaredTopics)) {
+ $log('Declare processor topic: %s', $topic->getTopicName());
+ $this->getContext()->declareTopic($topic);
+
+ $declaredTopics[$topic->getTopicName()] = true;
+ }
+
+ $log('Bind processor queue to topic: %s -> %s', $queue->getQueueName(), $topic->getTopicName());
+ $this->getContext()->bind($topic, $queue);
+ }
+ }
+
+ protected function createRouterTopic(): Destination
+ {
+ return $this->doCreateTopic(
+ $this->createTransportRouterTopicName($this->getConfig()->getRouterTopic(), true)
+ );
+ }
+
+ protected function createTransportRouterTopicName(string $name, bool $prefix): string
+ {
+ $name = parent::createTransportRouterTopicName($name, $prefix);
+
+ return str_replace('.', '_dot_', $name);
+ }
+
+ protected function createTransportQueueName(string $name, bool $prefix): string
+ {
+ $name = parent::createTransportQueueName($name, $prefix);
+
+ return str_replace('.', '_dot_', $name);
+ }
+}
diff --git a/pkg/enqueue/Client/Resources.php b/pkg/enqueue/Client/Resources.php
index b88b8227b..1207b64f8 100644
--- a/pkg/enqueue/Client/Resources.php
+++ b/pkg/enqueue/Client/Resources.php
@@ -12,6 +12,7 @@
use Enqueue\Client\Driver\RabbitMqStompDriver;
use Enqueue\Client\Driver\RdKafkaDriver;
use Enqueue\Client\Driver\RedisDriver;
+use Enqueue\Client\Driver\SnsQsDriver;
use Enqueue\Client\Driver\SqsDriver;
use Enqueue\Client\Driver\StompDriver;
@@ -92,6 +93,12 @@ public static function getKnownDrivers(): array
'requiredSchemeExtensions' => [],
'packages' => ['enqueue/enqueue', 'enqueue/sqs'],
];
+ $map[] = [
+ 'schemes' => ['snsqs'],
+ 'driverClass' => SnsQsDriver::class,
+ 'requiredSchemeExtensions' => [],
+ 'packages' => ['enqueue/enqueue', 'enqueue/sqs', 'enqueue/sns', 'enqueue/snsqs'],
+ ];
$map[] = [
'schemes' => ['stomp'],
'driverClass' => StompDriver::class,
diff --git a/pkg/enqueue/Consumption/Context/End.php b/pkg/enqueue/Consumption/Context/End.php
index 467f6d38c..ec8c1f7df 100644
--- a/pkg/enqueue/Consumption/Context/End.php
+++ b/pkg/enqueue/Consumption/Context/End.php
@@ -27,12 +27,23 @@ final class End
*/
private $logger;
- public function __construct(Context $context, int $startTime, int $endTime, LoggerInterface $logger)
- {
+ /**
+ * @var int
+ */
+ private $exitStatus;
+
+ public function __construct(
+ Context $context,
+ int $startTime,
+ int $endTime,
+ LoggerInterface $logger,
+ ?int $exitStatus = null
+ ) {
$this->context = $context;
$this->logger = $logger;
$this->startTime = $startTime;
$this->endTime = $endTime;
+ $this->exitStatus = $exitStatus;
}
public function getContext(): Context
@@ -60,4 +71,9 @@ public function getEndTime(): int
{
return $this->startTime;
}
+
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
}
diff --git a/pkg/enqueue/Consumption/Context/PostConsume.php b/pkg/enqueue/Consumption/Context/PostConsume.php
index 0add00336..a6f1d8375 100644
--- a/pkg/enqueue/Consumption/Context/PostConsume.php
+++ b/pkg/enqueue/Consumption/Context/PostConsume.php
@@ -43,6 +43,11 @@ final class PostConsume
*/
private $executionInterrupted;
+ /**
+ * @var int
+ */
+ private $exitStatus;
+
public function __construct(Context $context, SubscriptionConsumer $subscriptionConsumer, int $receivedMessagesCount, int $cycle, int $startTime, LoggerInterface $logger)
{
$this->context = $context;
@@ -85,13 +90,19 @@ public function getLogger(): LoggerInterface
return $this->logger;
}
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
+
public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}
- public function interruptExecution(): void
+ public function interruptExecution(?int $exitStatus = null): void
{
+ $this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
diff --git a/pkg/enqueue/Consumption/Context/PostMessageReceived.php b/pkg/enqueue/Consumption/Context/PostMessageReceived.php
index 423830e3d..91b532656 100644
--- a/pkg/enqueue/Consumption/Context/PostMessageReceived.php
+++ b/pkg/enqueue/Consumption/Context/PostMessageReceived.php
@@ -45,6 +45,11 @@ final class PostMessageReceived
*/
private $executionInterrupted;
+ /**
+ * @var int
+ */
+ private $exitStatus;
+
public function __construct(
Context $context,
Consumer $consumer,
@@ -89,20 +94,26 @@ public function getReceivedAt(): int
}
/**
- * @return Result|null|object|string
+ * @return Result|object|string|null
*/
public function getResult()
{
return $this->result;
}
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
+
public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}
- public function interruptExecution(): void
+ public function interruptExecution(?int $exitStatus = null): void
{
+ $this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
diff --git a/pkg/enqueue/Consumption/Context/PreConsume.php b/pkg/enqueue/Consumption/Context/PreConsume.php
index a6d83d534..77cc7d030 100644
--- a/pkg/enqueue/Consumption/Context/PreConsume.php
+++ b/pkg/enqueue/Consumption/Context/PreConsume.php
@@ -43,6 +43,11 @@ final class PreConsume
*/
private $executionInterrupted;
+ /**
+ * @var int
+ */
+ private $exitStatus;
+
public function __construct(Context $context, SubscriptionConsumer $subscriptionConsumer, LoggerInterface $logger, int $cycle, int $receiveTimeout, int $startTime)
{
$this->context = $context;
@@ -85,13 +90,19 @@ public function getStartTime(): int
return $this->startTime;
}
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
+
public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}
- public function interruptExecution(): void
+ public function interruptExecution(?int $exitStatus = null): void
{
+ $this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
diff --git a/pkg/enqueue/Consumption/Context/Start.php b/pkg/enqueue/Consumption/Context/Start.php
index cd9f2108e..84db29c44 100644
--- a/pkg/enqueue/Consumption/Context/Start.php
+++ b/pkg/enqueue/Consumption/Context/Start.php
@@ -38,6 +38,11 @@ final class Start
*/
private $executionInterrupted;
+ /**
+ * @var int
+ */
+ private $exitStatus;
+
/**
* @param BoundProcessor[] $processors
*/
@@ -105,13 +110,19 @@ public function changeBoundProcessors(array $processors): void
});
}
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
+
public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}
- public function interruptExecution(): void
+ public function interruptExecution(?int $exitStatus = null): void
{
+ $this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
diff --git a/pkg/enqueue/Consumption/Extension/ExitStatusExtension.php b/pkg/enqueue/Consumption/Extension/ExitStatusExtension.php
new file mode 100644
index 000000000..a43071241
--- /dev/null
+++ b/pkg/enqueue/Consumption/Extension/ExitStatusExtension.php
@@ -0,0 +1,24 @@
+exitStatus = $context->getExitStatus();
+ }
+
+ public function getExitStatus(): ?int
+ {
+ return $this->exitStatus;
+ }
+}
diff --git a/pkg/enqueue/Consumption/QueueConsumer.php b/pkg/enqueue/Consumption/QueueConsumer.php
index 3454b302d..30d438dcd 100644
--- a/pkg/enqueue/Consumption/QueueConsumer.php
+++ b/pkg/enqueue/Consumption/QueueConsumer.php
@@ -147,7 +147,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onStart($start);
if ($start->isExecutionInterrupted()) {
- $this->onEnd($extension, $startTime);
+ $this->onEnd($extension, $startTime, $start->getExitStatus());
return;
}
@@ -256,7 +256,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onPreConsume($preConsume);
if ($preConsume->isExecutionInterrupted()) {
- $this->onEnd($extension, $startTime, $subscriptionConsumer);
+ $this->onEnd($extension, $startTime, $preConsume->getExitStatus(), $subscriptionConsumer);
return;
}
@@ -267,7 +267,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onPostConsume($postConsume);
if ($interruptExecution || $postConsume->isExecutionInterrupted()) {
- $this->onEnd($extension, $startTime, $subscriptionConsumer);
+ $this->onEnd($extension, $startTime, $postConsume->getExitStatus(), $subscriptionConsumer);
return;
}
@@ -286,11 +286,12 @@ public function setFallbackSubscriptionConsumer(SubscriptionConsumer $fallbackSu
$this->fallbackSubscriptionConsumer = $fallbackSubscriptionConsumer;
}
- private function onEnd(ExtensionInterface $extension, int $startTime, SubscriptionConsumer $subscriptionConsumer = null): void
+ private function onEnd(ExtensionInterface $extension, int $startTime, ?int $exitStatus = null, SubscriptionConsumer $subscriptionConsumer = null): void
{
$endTime = (int) (microtime(true) * 1000);
- $extension->onEnd(new End($this->interopContext, $startTime, $endTime, $this->logger));
+ $endContext = new End($this->interopContext, $startTime, $endTime, $this->logger, $exitStatus);
+ $extension->onEnd($endContext);
if ($subscriptionConsumer) {
$subscriptionConsumer->unsubscribeAll();
@@ -298,7 +299,7 @@ private function onEnd(ExtensionInterface $extension, int $startTime, Subscripti
}
/**
- * The logic is similar to one in Symfony's ExceptionListener::.
+ * The logic is similar to one in Symfony's ExceptionListener::onKernelException().
*
* https://github.com/symfony/symfony/blob/cbe289517470eeea27162fd2d523eb29c95f775f/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php#L77
*/
@@ -316,18 +317,16 @@ private function onProcessorException(ExtensionInterface $extension, Consumer $c
return $result;
} catch (\Exception $e) {
- $wrapper = $e;
- while ($prev = $wrapper->getPrevious()) {
+ $prev = $e;
+ do {
if ($exception === $wrapper = $prev) {
throw $e;
}
- }
+ } while ($prev = $wrapper->getPrevious());
- if ($exception !== $wrapper) {
- $prev = new \ReflectionProperty('Exception', 'previous');
- $prev->setAccessible(true);
- $prev->setValue($wrapper, $exception);
- }
+ $prev = new \ReflectionProperty($wrapper instanceof \Exception ? \Exception::class : \Error::class, 'previous');
+ $prev->setAccessible(true);
+ $prev->setValue($wrapper, $exception);
throw $e;
}
diff --git a/pkg/enqueue/Resources.php b/pkg/enqueue/Resources.php
index a3d94da66..75884bab6 100644
--- a/pkg/enqueue/Resources.php
+++ b/pkg/enqueue/Resources.php
@@ -14,6 +14,7 @@
use Enqueue\Pheanstalk\PheanstalkConnectionFactory;
use Enqueue\RdKafka\RdKafkaConnectionFactory;
use Enqueue\Redis\RedisConnectionFactory;
+use Enqueue\SnsQs\SnsQsConnectionFactory;
use Enqueue\Sqs\SqsConnectionFactory;
use Enqueue\Stomp\StompConnectionFactory;
use Enqueue\Wamp\WampConnectionFactory;
@@ -155,6 +156,10 @@ public static function getKnownConnections(): array
'schemes' => ['sqs'],
'supportedSchemeExtensions' => [],
'package' => 'enqueue/sqs', ];
+ $map[SnsQsConnectionFactory::class] = [
+ 'schemes' => ['snsqs'],
+ 'supportedSchemeExtensions' => [],
+ 'package' => 'enqueue/snsqs', ];
$map[GpsConnectionFactory::class] = [
'schemes' => ['gps'],
'supportedSchemeExtensions' => [],
diff --git a/pkg/enqueue/Symfony/Client/ConsumeCommand.php b/pkg/enqueue/Symfony/Client/ConsumeCommand.php
index e6b25fc4f..ee8b394f6 100644
--- a/pkg/enqueue/Symfony/Client/ConsumeCommand.php
+++ b/pkg/enqueue/Symfony/Client/ConsumeCommand.php
@@ -4,6 +4,7 @@
use Enqueue\Client\DriverInterface;
use Enqueue\Consumption\ChainExtension;
+use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\Extension\LoggerExtension;
use Enqueue\Consumption\ExtensionInterface;
use Enqueue\Consumption\QueueConsumerInterface;
@@ -143,9 +144,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$consumer->bind($queue, $processor);
}
- $consumer->consume($this->getRuntimeExtensions($input, $output));
+ $runtimeExtensionChain = $this->getRuntimeExtensions($input, $output);
+ $exitStatusExtension = new ExitStatusExtension();
- return null;
+ $consumer->consume(new ChainExtension([$runtimeExtensionChain, $exitStatusExtension]));
+
+ return $exitStatusExtension->getExitStatus();
}
protected function getRuntimeExtensions(InputInterface $input, OutputInterface $output): ExtensionInterface
diff --git a/pkg/enqueue/Symfony/Consumption/ConsumeCommand.php b/pkg/enqueue/Symfony/Consumption/ConsumeCommand.php
index e65acc314..b10d19f68 100644
--- a/pkg/enqueue/Symfony/Consumption/ConsumeCommand.php
+++ b/pkg/enqueue/Symfony/Consumption/ConsumeCommand.php
@@ -3,6 +3,7 @@
namespace Enqueue\Symfony\Consumption;
use Enqueue\Consumption\ChainExtension;
+use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\QueueConsumerInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -75,9 +76,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
array_unshift($extensions, $loggerExtension);
}
+ $exitStatusExtension = new ExitStatusExtension();
+ array_unshift($extensions, $exitStatusExtension);
+
$consumer->consume(new ChainExtension($extensions));
- return null;
+ return $exitStatusExtension->getExitStatus();
}
private function getQueueConsumer(string $name): QueueConsumerInterface
diff --git a/pkg/enqueue/Tests/Consumption/QueueConsumerTest.php b/pkg/enqueue/Tests/Consumption/QueueConsumerTest.php
index 9b0111fda..b10339304 100644
--- a/pkg/enqueue/Tests/Consumption/QueueConsumerTest.php
+++ b/pkg/enqueue/Tests/Consumption/QueueConsumerTest.php
@@ -16,6 +16,7 @@
use Enqueue\Consumption\Context\ProcessorException;
use Enqueue\Consumption\Context\Start;
use Enqueue\Consumption\Exception\InvalidArgumentException;
+use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\ExtensionInterface;
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\Result;
@@ -1429,6 +1430,29 @@ public function testShouldCallProcessorAsMessageComeAlong()
$this->assertSame($fooConsumerStub, $actualContexts[2]->getConsumer());
}
+ public function testCaptureExitStatus()
+ {
+ $testExitCode = 5;
+
+ $stubExtension = $this->createExtension();
+
+ $stubExtension
+ ->expects($this->once())
+ ->method('onStart')
+ ->with($this->isInstanceOf(Start::class))
+ ->willReturnCallback(function (Start $context) use ($testExitCode) {
+ $context->interruptExecution($testExitCode);
+ })
+ ;
+
+ $exitExtension = new ExitStatusExtension();
+
+ $consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
+ $consumer->consume(new ChainExtension([$exitExtension]));
+
+ $this->assertEquals($testExitCode, $exitExtension->getExitStatus());
+ }
+
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
@@ -1508,7 +1532,7 @@ private function createExtension()
}
/**
- * @param null|mixed $queue
+ * @param mixed|null $queue
*
* @return \PHPUnit_Framework_MockObject_MockObject|Consumer
*/
diff --git a/pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php
index 9a45d424d..52bcd9a1e 100644
--- a/pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php
+++ b/pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php
@@ -8,11 +8,18 @@
use Enqueue\Client\Route;
use Enqueue\Client\RouteCollection;
use Enqueue\Consumption\ChainExtension;
+use Enqueue\Consumption\Context\Start;
+use Enqueue\Consumption\ExtensionInterface;
+use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\QueueConsumerInterface;
use Enqueue\Container\Container;
use Enqueue\Null\NullQueue;
use Enqueue\Symfony\Client\ConsumeCommand;
use Enqueue\Test\ClassExtensionTrait;
+use Interop\Queue\Consumer;
+use Interop\Queue\Context as InteropContext;
+use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
+use Interop\Queue\Queue;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
@@ -536,6 +543,49 @@ public function testShouldSkipQueueConsumptionAndUseCustomClientDestinationName(
]);
}
+ public function testShouldReturnExitStatusIfSet()
+ {
+ $testExitCode = 678;
+
+ $stubExtension = $this->createExtension();
+
+ $stubExtension
+ ->expects($this->once())
+ ->method('onStart')
+ ->with($this->isInstanceOf(Start::class))
+ ->willReturnCallback(function (Start $context) use ($testExitCode) {
+ $context->interruptExecution($testExitCode);
+ })
+ ;
+
+ $defaultQueue = new NullQueue('default');
+
+ $routeCollection = new RouteCollection([]);
+
+ $processor = $this->createDelegateProcessorMock();
+
+ $driver = $this->createDriverStub($routeCollection);
+ $driver
+ ->expects($this->exactly(1))
+ ->method('createQueue')
+ ->with('default', true)
+ ->willReturn($defaultQueue)
+ ;
+
+ $consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
+
+ $command = new ConsumeCommand(new Container([
+ 'enqueue.client.default.queue_consumer' => $consumer,
+ 'enqueue.client.default.driver' => $driver,
+ 'enqueue.client.default.delegate_processor' => $processor,
+ ]), 'default');
+
+ $tester = new CommandTester($command);
+ $tester->execute([]);
+
+ $this->assertEquals($testExitCode, $tester->getStatusCode());
+ }
+
/**
* @return \PHPUnit_Framework_MockObject_MockObject|DelegateProcessor
*/
@@ -572,4 +622,72 @@ private function createDriverStub(RouteCollection $routeCollection = null): Driv
return $driverMock;
}
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ private function createContextWithoutSubscriptionConsumerMock(): InteropContext
+ {
+ $contextMock = $this->createMock(InteropContext::class);
+ $contextMock
+ ->expects($this->any())
+ ->method('createSubscriptionConsumer')
+ ->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
+ ;
+
+ return $contextMock;
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|InteropContext
+ */
+ private function createContextStub(Consumer $consumer = null): InteropContext
+ {
+ $context = $this->createContextWithoutSubscriptionConsumerMock();
+ $context
+ ->expects($this->any())
+ ->method('createQueue')
+ ->willReturnCallback(function (string $queueName) {
+ return new NullQueue($queueName);
+ })
+ ;
+ $context
+ ->expects($this->any())
+ ->method('createConsumer')
+ ->willReturnCallback(function (Queue $queue) use ($consumer) {
+ return $consumer ?: $this->createConsumerStub($queue);
+ })
+ ;
+
+ return $context;
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
+ */
+ private function createExtension()
+ {
+ return $this->createMock(ExtensionInterface::class);
+ }
+
+ /**
+ * @param mixed|null $queue
+ *
+ * @return \PHPUnit_Framework_MockObject_MockObject|Consumer
+ */
+ private function createConsumerStub($queue = null): Consumer
+ {
+ if (is_string($queue)) {
+ $queue = new NullQueue($queue);
+ }
+
+ $consumerMock = $this->createMock(Consumer::class);
+ $consumerMock
+ ->expects($this->any())
+ ->method('getQueue')
+ ->willReturn($queue)
+ ;
+
+ return $consumerMock;
+ }
}
diff --git a/pkg/enqueue/Tests/Symfony/Consumption/ConsumeCommandTest.php b/pkg/enqueue/Tests/Symfony/Consumption/ConsumeCommandTest.php
index 6396568cb..88b826725 100644
--- a/pkg/enqueue/Tests/Symfony/Consumption/ConsumeCommandTest.php
+++ b/pkg/enqueue/Tests/Symfony/Consumption/ConsumeCommandTest.php
@@ -3,10 +3,18 @@
namespace Enqueue\Tests\Symfony\Consumption;
use Enqueue\Consumption\ChainExtension;
+use Enqueue\Consumption\Context\Start;
+use Enqueue\Consumption\ExtensionInterface;
+use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\QueueConsumerInterface;
use Enqueue\Container\Container;
+use Enqueue\Null\NullQueue;
use Enqueue\Symfony\Consumption\ConsumeCommand;
use Enqueue\Test\ClassExtensionTrait;
+use Interop\Queue\Consumer;
+use Interop\Queue\Context as InteropContext;
+use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
+use Interop\Queue\Queue;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
@@ -123,6 +131,34 @@ public function testThrowIfNotDefinedTransportRequested()
$tester->execute(['--transport' => 'not-defined']);
}
+ public function testShouldReturnExitStatusIfSet()
+ {
+ $testExitCode = 678;
+
+ $stubExtension = $this->createExtension();
+
+ $stubExtension
+ ->expects($this->once())
+ ->method('onStart')
+ ->with($this->isInstanceOf(Start::class))
+ ->willReturnCallback(function (Start $context) use ($testExitCode) {
+ $context->interruptExecution($testExitCode);
+ })
+ ;
+
+ $consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
+
+ $command = new ConsumeCommand(new Container([
+ 'enqueue.transport.default.queue_consumer' => $consumer,
+ ]), 'default');
+
+ $tester = new CommandTester($command);
+
+ $tester->execute([]);
+
+ $this->assertEquals($testExitCode, $tester->getStatusCode());
+ }
+
/**
* @return \PHPUnit_Framework_MockObject_MockObject|QueueConsumerInterface
*/
@@ -130,4 +166,72 @@ private function createQueueConsumerMock()
{
return $this->createMock(QueueConsumerInterface::class);
}
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ private function createContextWithoutSubscriptionConsumerMock(): InteropContext
+ {
+ $contextMock = $this->createMock(InteropContext::class);
+ $contextMock
+ ->expects($this->any())
+ ->method('createSubscriptionConsumer')
+ ->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
+ ;
+
+ return $contextMock;
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|InteropContext
+ */
+ private function createContextStub(Consumer $consumer = null): InteropContext
+ {
+ $context = $this->createContextWithoutSubscriptionConsumerMock();
+ $context
+ ->expects($this->any())
+ ->method('createQueue')
+ ->willReturnCallback(function (string $queueName) {
+ return new NullQueue($queueName);
+ })
+ ;
+ $context
+ ->expects($this->any())
+ ->method('createConsumer')
+ ->willReturnCallback(function (Queue $queue) use ($consumer) {
+ return $consumer ?: $this->createConsumerStub($queue);
+ })
+ ;
+
+ return $context;
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
+ */
+ private function createExtension()
+ {
+ return $this->createMock(ExtensionInterface::class);
+ }
+
+ /**
+ * @param mixed|null $queue
+ *
+ * @return \PHPUnit_Framework_MockObject_MockObject|Consumer
+ */
+ private function createConsumerStub($queue = null): Consumer
+ {
+ if (is_string($queue)) {
+ $queue = new NullQueue($queue);
+ }
+
+ $consumerMock = $this->createMock(Consumer::class);
+ $consumerMock
+ ->expects($this->any())
+ ->method('getQueue')
+ ->willReturn($queue)
+ ;
+
+ return $consumerMock;
+ }
}
diff --git a/pkg/enqueue/composer.json b/pkg/enqueue/composer.json
index 05dd1df2c..29cd7a847 100644
--- a/pkg/enqueue/composer.json
+++ b/pkg/enqueue/composer.json
@@ -41,7 +41,7 @@
"enqueue/dsn": "0.9.x-dev"
},
"suggest": {
- "symfony/console": "^2.8|^3|^4 If you want to use li commands",
+ "symfony/console": "^2.8|^3|^4 If you want to use cli commands",
"symfony/dependency-injection": "^3.4|^4",
"symfony/config": "^3.4|^4",
"enqueue/amqp-ext": "AMQP transport (based on php extension)",
diff --git a/pkg/fs/FsConsumer.php b/pkg/fs/FsConsumer.php
index 65ff12e61..46e7b96ba 100644
--- a/pkg/fs/FsConsumer.php
+++ b/pkg/fs/FsConsumer.php
@@ -90,7 +90,7 @@ public function receive(int $timeout = 0): ?Message
return null;
}
- usleep($this->pollingInterval);
+ usleep($this->pollingInterval * 1000);
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
return null;
diff --git a/pkg/simple-client/README.md b/pkg/simple-client/README.md
index d7054f1be..ddc024f0f 100644
--- a/pkg/simple-client/README.md
+++ b/pkg/simple-client/README.md
@@ -14,7 +14,7 @@ Enqueue is an MIT-licensed open source project with its ongoing development made
[](https://packagist.org/packages/enqueue/simple-client)
[](https://packagist.org/packages/enqueue/simple-client)
-The simple client takes Enqueue client classes and Symfony components and combines it to easy to use facade called `SimpleCLient`.
+The simple client takes Enqueue client classes and Symfony components and combines it to easy to use facade called `SimpleClient`.
## Resources
diff --git a/pkg/simple-client/SimpleClient.php b/pkg/simple-client/SimpleClient.php
index 52329a6c7..9158364b9 100644
--- a/pkg/simple-client/SimpleClient.php
+++ b/pkg/simple-client/SimpleClient.php
@@ -126,9 +126,9 @@ public function __construct($config, LoggerInterface $logger = null)
];
}
- $this->build(['enqueue' => $config]);
-
$this->logger = $logger ?: new NullLogger();
+
+ $this->build(['enqueue' => $config]);
}
/**
diff --git a/pkg/sns/.gitattributes b/pkg/sns/.gitattributes
new file mode 100644
index 000000000..bdf2dcb14
--- /dev/null
+++ b/pkg/sns/.gitattributes
@@ -0,0 +1,5 @@
+/Tests export-ignore
+.gitattributes export-ignore
+.gitignore export-ignore
+.travis.yml export-ignore
+phpunit.xml.dist export-ignore
diff --git a/pkg/sns/.gitignore b/pkg/sns/.gitignore
new file mode 100644
index 000000000..a770439e5
--- /dev/null
+++ b/pkg/sns/.gitignore
@@ -0,0 +1,6 @@
+*~
+/composer.lock
+/composer.phar
+/phpunit.xml
+/vendor/
+/.idea/
diff --git a/pkg/sns/.travis.yml b/pkg/sns/.travis.yml
new file mode 100644
index 000000000..9ed4fa123
--- /dev/null
+++ b/pkg/sns/.travis.yml
@@ -0,0 +1,21 @@
+sudo: false
+
+git:
+ depth: 10
+
+language: php
+
+php:
+ - '7.1'
+ - '7.2'
+
+cache:
+ directories:
+ - $HOME/.composer/cache
+
+install:
+ - composer self-update
+ - composer install --prefer-source
+
+script:
+ - vendor/bin/phpunit --exclude-group=functional
diff --git a/pkg/sns/LICENSE b/pkg/sns/LICENSE
new file mode 100644
index 000000000..20211e5fd
--- /dev/null
+++ b/pkg/sns/LICENSE
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+Copyright (c) 2018 Max Kotliar
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/pkg/sns/README.md b/pkg/sns/README.md
new file mode 100644
index 000000000..bcec08c1e
--- /dev/null
+++ b/pkg/sns/README.md
@@ -0,0 +1,28 @@
+Supporting Enqueue
+
+Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider:
+
+- [Become a sponsor](https://www.patreon.com/makasim)
+- [Become our client](http://forma-pro.com/)
+
+---
+
+# Amazon SNS Transport
+
+[](https://gitter.im/php-enqueue/Lobby)
+[](https://travis-ci.org/php-enqueue/sns)
+[](https://packagist.org/packages/enqueue/sns)
+[](https://packagist.org/packages/enqueue/sns)
+
+This is an implementation of Queue Interop specification. It allows you to send and consume message using [Amazon SNS](https://aws.amazon.com/sns/) service.
+
+## Resources
+
+* [Site](https://enqueue.forma-pro.com/)
+* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/sns.md)
+* [Questions](https://gitter.im/php-enqueue/Lobby)
+* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
+
+## License
+
+It is released under the [MIT License](LICENSE).
\ No newline at end of file
diff --git a/pkg/sns/SnsClient.php b/pkg/sns/SnsClient.php
new file mode 100644
index 000000000..2d33faf25
--- /dev/null
+++ b/pkg/sns/SnsClient.php
@@ -0,0 +1,145 @@
+inputClient = $inputClient;
+ }
+
+ public function createTopic(array $args): Result
+ {
+ return $this->callApi('createTopic', $args);
+ }
+
+ public function deleteTopic(string $topicArn): Result
+ {
+ return $this->callApi('DeleteTopic', [
+ 'TopicArn' => $topicArn,
+ ]);
+ }
+
+ public function publish(array $args): Result
+ {
+ return $this->callApi('publish', $args);
+ }
+
+ public function subscribe(array $args): Result
+ {
+ return $this->callApi('subscribe', $args);
+ }
+
+ public function unsubscribe(array $args): Result
+ {
+ return $this->callApi('unsubscribe', $args);
+ }
+
+ public function listSubscriptionsByTopic(array $args): Result
+ {
+ return $this->callApi('ListSubscriptionsByTopic', $args);
+ }
+
+ public function getAWSClient(): AwsSnsClient
+ {
+ $this->resolveClient();
+
+ if ($this->singleClient) {
+ return $this->singleClient;
+ }
+
+ if ($this->multiClient) {
+ $mr = new \ReflectionMethod($this->multiClient, 'getClientFromPool');
+ $mr->setAccessible(true);
+ $singleClient = $mr->invoke($this->multiClient, $this->multiClient->getRegion());
+ $mr->setAccessible(false);
+
+ return $singleClient;
+ }
+
+ throw new \LogicException('The multi or single client must be set');
+ }
+
+ private function callApi(string $name, array $args): Result
+ {
+ $this->resolveClient();
+
+ if ($this->singleClient) {
+ if (false == empty($args['@region'])) {
+ throw new \LogicException('Cannot send message to another region because transport is configured with single aws client');
+ }
+
+ unset($args['@region']);
+
+ return call_user_func([$this->singleClient, $name], $args);
+ }
+
+ if ($this->multiClient) {
+ return call_user_func([$this->multiClient, $name], $args);
+ }
+
+ throw new \LogicException('The multi or single client must be set');
+ }
+
+ private function resolveClient(): void
+ {
+ if ($this->singleClient || $this->multiClient) {
+ return;
+ }
+
+ $client = $this->inputClient;
+ if ($client instanceof MultiRegionClient) {
+ $this->multiClient = $client;
+
+ return;
+ } elseif ($client instanceof AwsSnsClient) {
+ $this->singleClient = $client;
+
+ return;
+ } elseif (is_callable($client)) {
+ $client = call_user_func($client);
+ if ($client instanceof MultiRegionClient) {
+ $this->multiClient = $client;
+
+ return;
+ }
+ if ($client instanceof AwsSnsClient) {
+ $this->singleClient = $client;
+
+ return;
+ }
+ }
+
+ throw new \LogicException(sprintf(
+ 'The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"',
+ AwsSnsClient::class,
+ MultiRegionClient::class,
+ is_object($client) ? get_class($client) : gettype($client)
+ ));
+ }
+}
diff --git a/pkg/sns/SnsConnectionFactory.php b/pkg/sns/SnsConnectionFactory.php
new file mode 100644
index 000000000..ffef39803
--- /dev/null
+++ b/pkg/sns/SnsConnectionFactory.php
@@ -0,0 +1,149 @@
+ null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'region' => null, (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
+ * 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
+ * 'lazy' => true, Enable lazy connection (boolean)
+ * 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
+ * ].
+ *
+ * or
+ *
+ * sns:
+ * sns::?key=aKey&secret=aSecret&token=aToken
+ *
+ * @param array|string|SnsClient|null $config
+ */
+ public function __construct($config = 'sns:')
+ {
+ if ($config instanceof AwsSnsClient) {
+ $this->client = new SnsClient($config);
+ $this->config = ['lazy' => false] + $this->defaultConfig();
+
+ return;
+ }
+
+ if (empty($config)) {
+ $config = [];
+ } elseif (is_string($config)) {
+ $config = $this->parseDsn($config);
+ } elseif (is_array($config)) {
+ if (array_key_exists('dsn', $config)) {
+ $config = array_replace_recursive($config, $this->parseDsn($config['dsn']));
+
+ unset($config['dsn']);
+ }
+ } else {
+ throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class));
+ }
+
+ $this->config = array_replace($this->defaultConfig(), $config);
+ }
+
+ /**
+ * @return SnsContext
+ */
+ public function createContext(): Context
+ {
+ return new SnsContext($this->establishConnection(), $this->config);
+ }
+
+ private function establishConnection(): SnsClient
+ {
+ if ($this->client) {
+ return $this->client;
+ }
+
+ $config = [
+ 'version' => $this->config['version'],
+ 'region' => $this->config['region'],
+ ];
+
+ if (isset($this->config['endpoint'])) {
+ $config['endpoint'] = $this->config['endpoint'];
+ }
+
+ if ($this->config['key'] && $this->config['secret']) {
+ $config['credentials'] = [
+ 'key' => $this->config['key'],
+ 'secret' => $this->config['secret'],
+ ];
+
+ if ($this->config['token']) {
+ $config['credentials']['token'] = $this->config['token'];
+ }
+ }
+
+ $establishConnection = function () use ($config) {
+ return (new Sdk(['Sns' => $config]))->createMultiRegionSns();
+ };
+
+ $this->client = $this->config['lazy'] ?
+ new SnsClient($establishConnection) :
+ new SnsClient($establishConnection())
+ ;
+
+ return $this->client;
+ }
+
+ private function parseDsn(string $dsn): array
+ {
+ $dsn = Dsn::parseFirst($dsn);
+
+ if ('sns' !== $dsn->getSchemeProtocol()) {
+ throw new \LogicException(sprintf(
+ 'The given scheme protocol "%s" is not supported. It must be "sns"',
+ $dsn->getSchemeProtocol()
+ ));
+ }
+
+ return array_filter(array_replace($dsn->getQuery(), [
+ 'key' => $dsn->getString('key'),
+ 'secret' => $dsn->getString('secret'),
+ 'token' => $dsn->getString('token'),
+ 'region' => $dsn->getString('region'),
+ 'version' => $dsn->getString('version'),
+ 'lazy' => $dsn->getBool('lazy'),
+ 'endpoint' => $dsn->getString('endpoint'),
+ ]), function ($value) { return null !== $value; });
+ }
+
+ private function defaultConfig(): array
+ {
+ return [
+ 'key' => null,
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => true,
+ 'endpoint' => null,
+ ];
+ }
+}
diff --git a/pkg/sns/SnsContext.php b/pkg/sns/SnsContext.php
new file mode 100644
index 000000000..b489f8d57
--- /dev/null
+++ b/pkg/sns/SnsContext.php
@@ -0,0 +1,200 @@
+client = $client;
+ $this->config = $config;
+
+ $this->topicArns = [];
+ }
+
+ /**
+ * @return SnsMessage
+ */
+ public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
+ {
+ return new SnsMessage($body, $properties, $headers);
+ }
+
+ /**
+ * @return SnsDestination
+ */
+ public function createTopic(string $topicName): Topic
+ {
+ return new SnsDestination($topicName);
+ }
+
+ /**
+ * @return SnsDestination
+ */
+ public function createQueue(string $queueName): Queue
+ {
+ return new SnsDestination($queueName);
+ }
+
+ public function declareTopic(SnsDestination $destination): void
+ {
+ $result = $this->client->createTopic([
+ 'Attributes' => $destination->getAttributes(),
+ 'Name' => $destination->getQueueName(),
+ ]);
+
+ if (false == $result->hasKey('TopicArn')) {
+ throw new \RuntimeException(sprintf('Cannot create topic. topicName: "%s"', $destination->getTopicName()));
+ }
+
+ $this->topicArns[$destination->getTopicName()] = (string) $result->get('TopicArn');
+ }
+
+ public function deleteTopic(SnsDestination $destination): void
+ {
+ $this->client->deleteTopic($this->getTopicArn($destination));
+
+ unset($this->topicArns[$destination->getTopicName()]);
+ }
+
+ public function subscribe(SnsSubscribe $subscribe): void
+ {
+ foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
+ if ($subscription['Protocol'] === $subscribe->getProtocol()
+ && $subscription['Endpoint'] === $subscribe->getEndpoint()) {
+ return;
+ }
+ }
+
+ $this->client->subscribe([
+ 'Attributes' => $subscribe->getAttributes(),
+ 'Endpoint' => $subscribe->getEndpoint(),
+ 'Protocol' => $subscribe->getProtocol(),
+ 'ReturnSubscriptionArn' => $subscribe->isReturnSubscriptionArn(),
+ 'TopicArn' => $this->getTopicArn($subscribe->getTopic()),
+ ]);
+ }
+
+ public function unsubscibe(SnsUnsubscribe $unsubscribe): void
+ {
+ foreach ($this->getSubscriptions($unsubscribe->getTopic()) as $subscription) {
+ if ($subscription['Protocol'] != $unsubscribe->getProtocol()) {
+ continue;
+ }
+
+ if ($subscription['Endpoint'] != $unsubscribe->getEndpoint()) {
+ continue;
+ }
+
+ $this->client->unsubscribe([
+ 'SubscriptionArn' => $subscription['SubscriptionArn'],
+ ]);
+ }
+ }
+
+ public function getSubscriptions(SnsDestination $destination): array
+ {
+ $args = [
+ 'TopicArn' => $this->getTopicArn($destination),
+ ];
+
+ $subscriptions = [];
+ while (true) {
+ $result = $this->client->listSubscriptionsByTopic($args);
+
+ $subscriptions = array_merge($subscriptions, $result->get('Subscriptions'));
+
+ if (false == $result->hasKey('NextToken')) {
+ break;
+ }
+
+ $args['NextToken'] = $result->get('NextToken');
+ }
+
+ return $subscriptions;
+ }
+
+ public function getTopicArn(SnsDestination $destination): string
+ {
+ if (false == array_key_exists($destination->getTopicName(), $this->topicArns)) {
+ $this->declareTopic($destination);
+ }
+
+ return $this->topicArns[$destination->getTopicName()];
+ }
+
+ public function createTemporaryQueue(): Queue
+ {
+ throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ /**
+ * @return SnsProducer
+ */
+ public function createProducer(): Producer
+ {
+ return new SnsProducer($this);
+ }
+
+ /**
+ * @param SnsDestination $destination
+ */
+ public function createConsumer(Destination $destination): Consumer
+ {
+ throw new \LogicException('SNS transport does not support consumption. You should consider using SQS instead.');
+ }
+
+ public function close(): void
+ {
+ }
+
+ /**
+ * @param SnsDestination $queue
+ */
+ public function purgeQueue(Queue $queue): void
+ {
+ PurgeQueueNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function createSubscriptionConsumer(): SubscriptionConsumer
+ {
+ throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function getAwsSnsClient(): AwsSnsClient
+ {
+ return $this->client->getAWSClient();
+ }
+
+ public function getSnsClient(): SnsClient
+ {
+ return $this->client;
+ }
+}
diff --git a/pkg/sns/SnsDestination.php b/pkg/sns/SnsDestination.php
new file mode 100644
index 000000000..0db9d914e
--- /dev/null
+++ b/pkg/sns/SnsDestination.php
@@ -0,0 +1,91 @@
+name = $name;
+ $this->attributes = [];
+ }
+
+ public function getQueueName(): string
+ {
+ return $this->name;
+ }
+
+ public function getTopicName(): string
+ {
+ return $this->name;
+ }
+
+ /**
+ * The policy that defines who can access your topic. By default, only the topic owner can publish or subscribe to the topic.
+ */
+ public function setPolicy(string $policy = null): void
+ {
+ $this->setAttribute('Policy', $policy);
+ }
+
+ public function getPolicy(): ?string
+ {
+ return $this->getAttribute('Policy');
+ }
+
+ /**
+ * The display name to use for a topic with SMS subscriptions.
+ */
+ public function setDisplayName(string $displayName = null): void
+ {
+ $this->setAttribute('DisplayName', $displayName);
+ }
+
+ public function getDisplayName(): ?string
+ {
+ return $this->getAttribute('DisplayName');
+ }
+
+ /**
+ * The display name to use for a topic with SMS subscriptions.
+ */
+ public function setDeliveryPolicy(int $deliveryPolicy = null): void
+ {
+ $this->setAttribute('DeliveryPolicy', $deliveryPolicy);
+ }
+
+ public function getDeliveryPolicy(): ?int
+ {
+ return $this->getAttribute('DeliveryPolicy');
+ }
+
+ public function getAttributes(): array
+ {
+ return $this->attributes;
+ }
+
+ private function getAttribute(string $name, $default = null)
+ {
+ return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
+ }
+
+ private function setAttribute(string $name, $value): void
+ {
+ if (null == $value) {
+ unset($this->attributes[$name]);
+ } else {
+ $this->attributes[$name] = $value;
+ }
+ }
+}
diff --git a/pkg/sns/SnsMessage.php b/pkg/sns/SnsMessage.php
new file mode 100644
index 000000000..7eb3c0d83
--- /dev/null
+++ b/pkg/sns/SnsMessage.php
@@ -0,0 +1,33 @@
+body = $body;
+ $this->properties = $properties;
+ $this->headers = $headers;
+ $this->redelivered = false;
+ }
+
+ public function getSnsMessageId(): ?string
+ {
+ return $this->snsMessageId;
+ }
+
+ public function setSnsMessageId(?string $snsMessageId): void
+ {
+ $this->snsMessageId = $snsMessageId;
+ }
+}
diff --git a/pkg/sns/SnsProducer.php b/pkg/sns/SnsProducer.php
new file mode 100644
index 000000000..b702d10c3
--- /dev/null
+++ b/pkg/sns/SnsProducer.php
@@ -0,0 +1,119 @@
+context = $context;
+ }
+
+ /**
+ * @param SnsDestination $destination
+ * @param SnsMessage $message
+ */
+ public function send(Destination $destination, Message $message): void
+ {
+ InvalidDestinationException::assertDestinationInstanceOf($destination, SnsDestination::class);
+ InvalidMessageException::assertMessageInstanceOf($message, SnsMessage::class);
+
+ $body = $message->getBody();
+ if (empty($body)) {
+ throw new InvalidMessageException('The message body must be a non-empty string.');
+ }
+
+ $topicArn = $this->context->getTopicArn($destination);
+
+ $arguments = [
+ 'Message' => $message->getBody(),
+ 'MessageAttributes' => [
+ 'Headers' => [
+ 'DataType' => 'String',
+ 'StringValue' => json_encode([$message->getHeaders(), $message->getProperties()]),
+ ],
+ ],
+ 'TopicArn' => $topicArn,
+ ];
+
+ $result = $this->context->getSnsClient()->publish($arguments);
+
+ if (false == $result->hasKey('MessageId')) {
+ throw new \RuntimeException('Message was not sent');
+ }
+
+ $message->setSnsMessageId((string) $result->get('MessageId'));
+ }
+
+ /**
+ * @return SnsProducer
+ */
+ public function setDeliveryDelay(int $deliveryDelay = null): Producer
+ {
+ if (null === $deliveryDelay) {
+ return $this;
+ }
+
+ DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function getDeliveryDelay(): ?int
+ {
+ return $this->deliveryDelay;
+ }
+
+ /**
+ * @return SnsProducer
+ */
+ public function setPriority(int $priority = null): Producer
+ {
+ if (null === $priority) {
+ return $this;
+ }
+
+ throw PriorityNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function getPriority(): ?int
+ {
+ return null;
+ }
+
+ /**
+ * @return SnsProducer
+ */
+ public function setTimeToLive(int $timeToLive = null): Producer
+ {
+ if (null === $timeToLive) {
+ return $this;
+ }
+
+ throw TimeToLiveNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function getTimeToLive(): ?int
+ {
+ return null;
+ }
+}
diff --git a/pkg/sns/SnsSubscribe.php b/pkg/sns/SnsSubscribe.php
new file mode 100644
index 000000000..70addb7e0
--- /dev/null
+++ b/pkg/sns/SnsSubscribe.php
@@ -0,0 +1,74 @@
+topic = $topic;
+ $this->endpoint = $endpoint;
+ $this->protocol = $protocol;
+ $this->returnSubscriptionArn = $returnSubscriptionArn;
+ $this->attributes = $attributes;
+ }
+
+ public function getTopic(): SnsDestination
+ {
+ return $this->topic;
+ }
+
+ public function getEndpoint(): string
+ {
+ return $this->endpoint;
+ }
+
+ public function getProtocol(): string
+ {
+ return $this->protocol;
+ }
+
+ public function isReturnSubscriptionArn(): bool
+ {
+ return $this->returnSubscriptionArn;
+ }
+
+ public function getAttributes(): array
+ {
+ return $this->attributes;
+ }
+}
diff --git a/pkg/sns/SnsUnsubscribe.php b/pkg/sns/SnsUnsubscribe.php
new file mode 100644
index 000000000..cd5733651
--- /dev/null
+++ b/pkg/sns/SnsUnsubscribe.php
@@ -0,0 +1,48 @@
+topic = $topic;
+ $this->endpoint = $endpoint;
+ $this->protocol = $protocol;
+ }
+
+ public function getTopic(): SnsDestination
+ {
+ return $this->topic;
+ }
+
+ public function getEndpoint(): string
+ {
+ return $this->endpoint;
+ }
+
+ public function getProtocol(): string
+ {
+ return $this->protocol;
+ }
+}
diff --git a/pkg/sns/Tests/SnsClientTest.php b/pkg/sns/Tests/SnsClientTest.php
new file mode 100644
index 000000000..e56a18790
--- /dev/null
+++ b/pkg/sns/Tests/SnsClientTest.php
@@ -0,0 +1,229 @@
+ [
+ 'key' => '',
+ 'secret' => '',
+ 'token' => '',
+ 'region' => '',
+ 'version' => '2010-03-31',
+ 'endpoint' => 'http://localhost',
+ ]]))->createSns();
+
+ $client = new SnsClient($awsClient);
+
+ $this->assertSame($awsClient, $client->getAWSClient());
+ }
+
+ public function testShouldAllowGetAwsClientIfMultipleClientProvided()
+ {
+ $awsClient = (new Sdk(['Sns' => [
+ 'key' => '',
+ 'secret' => '',
+ 'token' => '',
+ 'region' => '',
+ 'version' => '2010-03-31',
+ 'endpoint' => 'http://localhost',
+ ]]))->createMultiRegionSns();
+
+ $client = new SnsClient($awsClient);
+
+ $this->assertInstanceOf(AwsSnsClient::class, $client->getAWSClient());
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ * @dataProvider provideApiCallsMultipleClient
+ */
+ public function testApiCall(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $awsClient = $this->getMockBuilder($awsClientClass)
+ ->disableOriginalConstructor()
+ ->setMethods([$method])
+ ->getMock();
+ $awsClient
+ ->expects($this->once())
+ ->method($method)
+ ->with($this->identicalTo($args))
+ ->willReturn(new Result($result));
+
+ $client = new SnsClient($awsClient);
+
+ $actualResult = $client->{$method}($args);
+
+ $this->assertInstanceOf(Result::class, $actualResult);
+ $this->assertSame($result, $actualResult->toArray());
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ * @dataProvider provideApiCallsMultipleClient
+ */
+ public function testLazyApiCall(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $awsClient = $this->getMockBuilder($awsClientClass)
+ ->disableOriginalConstructor()
+ ->setMethods([$method])
+ ->getMock();
+ $awsClient
+ ->expects($this->once())
+ ->method($method)
+ ->with($this->identicalTo($args))
+ ->willReturn(new Result($result));
+
+ $client = new SnsClient(function () use ($awsClient) {
+ return $awsClient;
+ });
+
+ $actualResult = $client->{$method}($args);
+
+ $this->assertInstanceOf(Result::class, $actualResult);
+ $this->assertSame($result, $actualResult->toArray());
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ * @dataProvider provideApiCallsMultipleClient
+ */
+ public function testThrowIfInvalidInputClientApiCall(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $client = new SnsClient(new \stdClass());
+
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('The input client must be an instance of "Aws\Sns\SnsClient" or "Aws\MultiRegionClient" or a callable that returns one of those. Got "stdClass"');
+ $client->{$method}($args);
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ * @dataProvider provideApiCallsMultipleClient
+ */
+ public function testThrowIfInvalidLazyInputClientApiCall(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $client = new SnsClient(function () { return new \stdClass(); });
+
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('The input client must be an instance of "Aws\Sns\SnsClient" or "Aws\MultiRegionClient" or a callable that returns one of those. Got "stdClass"');
+ $client->{$method}($args);
+ }
+
+ /**
+ * @dataProvider provideApiCallsMultipleClient
+ */
+ public function testApiCallWithMultiClientAndCustomRegion(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $args['@region'] = 'theRegion';
+
+ $awsClient = $this->getMockBuilder($awsClientClass)
+ ->disableOriginalConstructor()
+ ->setMethods([$method])
+ ->getMock();
+ $awsClient
+ ->expects($this->once())
+ ->method($method)
+ ->with($this->identicalTo($args))
+ ->willReturn(new Result($result));
+
+ $client = new SnsClient($awsClient);
+
+ $actualResult = $client->{$method}($args);
+
+ $this->assertInstanceOf(Result::class, $actualResult);
+ $this->assertSame($result, $actualResult->toArray());
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ */
+ public function testApiCallWithSingleClientAndCustomRegion(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $args['@region'] = 'theRegion';
+
+ $awsClient = $this->getMockBuilder($awsClientClass)
+ ->disableOriginalConstructor()
+ ->setMethods([$method])
+ ->getMock();
+ $awsClient
+ ->expects($this->never())
+ ->method($method)
+ ;
+
+ $client = new SnsClient($awsClient);
+
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('Cannot send message to another region because transport is configured with single aws client');
+ $client->{$method}($args);
+ }
+
+ /**
+ * @dataProvider provideApiCallsSingleClient
+ */
+ public function testApiCallWithMultiClientAndEmptyCustomRegion(string $method, array $args, array $result, string $awsClientClass)
+ {
+ $expectedArgs = $args;
+ $args['@region'] = '';
+
+ $awsClient = $this->getMockBuilder($awsClientClass)
+ ->disableOriginalConstructor()
+ ->setMethods([$method])
+ ->getMock();
+ $awsClient
+ ->expects($this->once())
+ ->method($method)
+ ->with($this->identicalTo($expectedArgs))
+ ->willReturn(new Result($result));
+
+ $client = new SnsClient($awsClient);
+
+ $actualResult = $client->{$method}($args);
+
+ $this->assertInstanceOf(Result::class, $actualResult);
+ $this->assertSame($result, $actualResult->toArray());
+ }
+
+ public function provideApiCallsSingleClient()
+ {
+ yield [
+ 'createTopic',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ AwsSnsClient::class,
+ ];
+
+ yield [
+ 'publish',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ AwsSnsClient::class,
+ ];
+ }
+
+ public function provideApiCallsMultipleClient()
+ {
+ yield [
+ 'createTopic',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ MultiRegionClient::class,
+ ];
+
+ yield [
+ 'publish',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ MultiRegionClient::class,
+ ];
+ }
+}
diff --git a/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php b/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php
new file mode 100644
index 000000000..d9cbff390
--- /dev/null
+++ b/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php
@@ -0,0 +1,152 @@
+expectException(\LogicException::class);
+ $this->expectExceptionMessage('The config must be either an array of options, a DSN string, null or instance of Aws\Sns\SnsClient');
+
+ new SnsConnectionFactory(new \stdClass());
+ }
+
+ public function testThrowIfSchemeIsNotAmqp()
+ {
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('The given scheme protocol "http" is not supported. It must be "sns"');
+
+ new SnsConnectionFactory('http://example.com');
+ }
+
+ public function testThrowIfDsnCouldNotBeParsed()
+ {
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('The DSN is invalid.');
+
+ new SnsConnectionFactory('foo');
+ }
+
+ /**
+ * @dataProvider provideConfigs
+ *
+ * @param mixed $config
+ * @param mixed $expectedConfig
+ */
+ public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
+ {
+ $factory = new SnsConnectionFactory($config);
+
+ $this->assertAttributeEquals($expectedConfig, 'config', $factory);
+ }
+
+ public static function provideConfigs()
+ {
+ yield [
+ null,
+ [
+ 'key' => null,
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => true,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ 'sns:',
+ [
+ 'key' => null,
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => true,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ [],
+ [
+ 'key' => null,
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => true,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ 'sns:?key=theKey&secret=theSecret&token=theToken&lazy=0',
+ [
+ 'key' => 'theKey',
+ 'secret' => 'theSecret',
+ 'token' => 'theToken',
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => false,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ ['dsn' => 'sns:?key=theKey&secret=theSecret&token=theToken&lazy=0'],
+ [
+ 'key' => 'theKey',
+ 'secret' => 'theSecret',
+ 'token' => 'theToken',
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => false,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ ['key' => 'theKey', 'secret' => 'theSecret', 'token' => 'theToken', 'lazy' => false],
+ [
+ 'key' => 'theKey',
+ 'secret' => 'theSecret',
+ 'token' => 'theToken',
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => false,
+ 'endpoint' => null,
+ ],
+ ];
+
+ yield [
+ [
+ 'key' => 'theKey',
+ 'secret' => 'theSecret',
+ 'token' => 'theToken',
+ 'lazy' => false,
+ 'endpoint' => 'http://localstack:1111',
+ ],
+ [
+ 'key' => 'theKey',
+ 'secret' => 'theSecret',
+ 'token' => 'theToken',
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'lazy' => false,
+ 'endpoint' => 'http://localstack:1111',
+ ],
+ ];
+ }
+}
diff --git a/pkg/sns/Tests/SnsConnectionFactoryTest.php b/pkg/sns/Tests/SnsConnectionFactoryTest.php
new file mode 100644
index 000000000..104c4dc18
--- /dev/null
+++ b/pkg/sns/Tests/SnsConnectionFactoryTest.php
@@ -0,0 +1,79 @@
+assertClassImplements(ConnectionFactory::class, SnsConnectionFactory::class);
+ }
+
+ public function testCouldBeConstructedWithEmptyConfiguration()
+ {
+ $factory = new SnsConnectionFactory([]);
+
+ $this->assertAttributeEquals([
+ 'lazy' => true,
+ 'key' => null,
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'endpoint' => null,
+ ], 'config', $factory);
+ }
+
+ public function testCouldBeConstructedWithCustomConfiguration()
+ {
+ $factory = new SnsConnectionFactory(['key' => 'theKey']);
+
+ $this->assertAttributeEquals([
+ 'lazy' => true,
+ 'key' => 'theKey',
+ 'secret' => null,
+ 'token' => null,
+ 'region' => null,
+ 'version' => '2010-03-31',
+ 'endpoint' => null,
+ ], 'config', $factory);
+ }
+
+ public function testCouldBeConstructedWithClient()
+ {
+ $awsClient = $this->createMock(AwsSnsClient::class);
+
+ $factory = new SnsConnectionFactory($awsClient);
+
+ $context = $factory->createContext();
+
+ $this->assertInstanceOf(SnsContext::class, $context);
+
+ $client = $this->readAttribute($context, 'client');
+ $this->assertInstanceOf(SnsClient::class, $client);
+ $this->assertAttributeSame($awsClient, 'inputClient', $client);
+ }
+
+ public function testShouldCreateLazyContext()
+ {
+ $factory = new SnsConnectionFactory(['lazy' => true]);
+
+ $context = $factory->createContext();
+
+ $this->assertInstanceOf(SnsContext::class, $context);
+
+ $client = $this->readAttribute($context, 'client');
+ $this->assertInstanceOf(SnsClient::class, $client);
+ $this->assertAttributeInstanceOf(\Closure::class, 'inputClient', $client);
+ }
+}
diff --git a/pkg/sns/Tests/SnsDestinationTest.php b/pkg/sns/Tests/SnsDestinationTest.php
new file mode 100644
index 000000000..c9f9669e7
--- /dev/null
+++ b/pkg/sns/Tests/SnsDestinationTest.php
@@ -0,0 +1,52 @@
+assertClassImplements(Topic::class, SnsDestination::class);
+ $this->assertClassImplements(Queue::class, SnsDestination::class);
+ }
+
+ public function testShouldReturnNameSetInConstructor()
+ {
+ $destination = new SnsDestination('aDestinationName');
+
+ $this->assertSame('aDestinationName', $destination->getQueueName());
+ $this->assertSame('aDestinationName', $destination->getTopicName());
+ }
+
+ public function testCouldSetPolicyAttribute()
+ {
+ $destination = new SnsDestination('aDestinationName');
+ $destination->setPolicy('thePolicy');
+
+ $this->assertSame(['Policy' => 'thePolicy'], $destination->getAttributes());
+ }
+
+ public function testCouldSetDisplayNameAttribute()
+ {
+ $destination = new SnsDestination('aDestinationName');
+ $destination->setDisplayName('theDisplayName');
+
+ $this->assertSame(['DisplayName' => 'theDisplayName'], $destination->getAttributes());
+ }
+
+ public function testCouldSetDeliveryPolicyAttribute()
+ {
+ $destination = new SnsDestination('aDestinationName');
+ $destination->setDeliveryPolicy(123);
+
+ $this->assertSame(['DeliveryPolicy' => 123], $destination->getAttributes());
+ }
+}
diff --git a/pkg/sns/Tests/SnsProducerTest.php b/pkg/sns/Tests/SnsProducerTest.php
new file mode 100644
index 000000000..f5a039702
--- /dev/null
+++ b/pkg/sns/Tests/SnsProducerTest.php
@@ -0,0 +1,144 @@
+assertClassImplements(Producer::class, SnsProducer::class);
+ }
+
+ public function testCouldBeConstructedWithRequiredArguments()
+ {
+ new SnsProducer($this->createSnsContextMock());
+ }
+
+ public function testShouldThrowIfBodyOfInvalidType()
+ {
+ $this->expectException(InvalidMessageException::class);
+ $this->expectExceptionMessage('The message body must be a non-empty string.');
+
+ $producer = new SnsProducer($this->createSnsContextMock());
+
+ $message = new SnsMessage('');
+
+ $producer->send(new SnsDestination(''), $message);
+ }
+
+ public function testShouldThrowIfDestinationOfInvalidType()
+ {
+ $this->expectException(InvalidDestinationException::class);
+ $this->expectExceptionMessage('The destination must be an instance of Enqueue\Sns\SnsDestination but got Mock_Destinat');
+
+ $producer = new SnsProducer($this->createSnsContextMock());
+
+ $producer->send($this->createMock(Destination::class), new SnsMessage());
+ }
+
+ public function testShouldThrowIfPublishFailed()
+ {
+ $destination = new SnsDestination('queue-name');
+
+ $client = $this->createSnsClientMock();
+ $client
+ ->expects($this->once())
+ ->method('publish')
+ ->willReturn(new Result())
+ ;
+
+ $context = $this->createSnsContextMock();
+ $context
+ ->expects($this->once())
+ ->method('getTopicArn')
+ ->with($this->identicalTo($destination))
+ ->willReturn('theTopicArn')
+ ;
+ $context
+ ->expects($this->once())
+ ->method('getSnsClient')
+ ->will($this->returnValue($client))
+ ;
+
+ $message = new SnsMessage('foo');
+
+ $this->expectException(\RuntimeException::class);
+ $this->expectExceptionMessage('Message was not sent');
+
+ $producer = new SnsProducer($context);
+ $producer->send($destination, $message);
+ }
+
+ public function testShouldPublish()
+ {
+ $destination = new SnsDestination('queue-name');
+
+ $expectedArguments = [
+ 'Message' => 'theBody',
+ 'MessageAttributes' => [
+ 'Headers' => [
+ 'DataType' => 'String',
+ 'StringValue' => '[{"hkey":"hvaleu"},{"key":"value"}]',
+ ],
+ ],
+ 'TopicArn' => 'theTopicArn',
+ ];
+
+ $client = $this->createSnsClientMock();
+ $client
+ ->expects($this->once())
+ ->method('publish')
+ ->with($this->identicalTo($expectedArguments))
+ ->willReturn(new Result(['MessageId' => 'theMessageId']))
+ ;
+
+ $context = $this->createSnsContextMock();
+ $context
+ ->expects($this->once())
+ ->method('getTopicArn')
+ ->with($this->identicalTo($destination))
+ ->willReturn('theTopicArn')
+ ;
+ $context
+ ->expects($this->once())
+ ->method('getSnsClient')
+ ->will($this->returnValue($client))
+ ;
+
+ $message = new SnsMessage('theBody', ['key' => 'value'], ['hkey' => 'hvaleu']);
+
+ $producer = new SnsProducer($context);
+ $producer->send($destination, $message);
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|SnsContext
+ */
+ private function createSnsContextMock(): SnsContext
+ {
+ return $this->createMock(SnsContext::class);
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|SnsClient
+ */
+ private function createSnsClientMock(): SnsClient
+ {
+ return $this->createMock(SnsClient::class);
+ }
+}
diff --git a/pkg/sns/Tests/Spec/SnsConnectionFactoryTest.php b/pkg/sns/Tests/Spec/SnsConnectionFactoryTest.php
new file mode 100644
index 000000000..20008bc04
--- /dev/null
+++ b/pkg/sns/Tests/Spec/SnsConnectionFactoryTest.php
@@ -0,0 +1,18 @@
+expectException(\LogicException::class);
+ $this->expectExceptionMessage('SNS transport does not support consumption. You should consider using SQS instead.');
+
+ parent::testShouldCreateConsumerOnCreateConsumerMethodCall();
+ }
+
+ protected function createContext()
+ {
+ $client = $this->createMock(SnsClient::class);
+
+ return new SnsContext($client, []);
+ }
+}
diff --git a/pkg/sns/Tests/Spec/SnsMessageTest.php b/pkg/sns/Tests/Spec/SnsMessageTest.php
new file mode 100644
index 000000000..af24344e6
--- /dev/null
+++ b/pkg/sns/Tests/Spec/SnsMessageTest.php
@@ -0,0 +1,14 @@
+createMock(SnsContext::class));
+ }
+}
diff --git a/pkg/sns/Tests/Spec/SnsQueueTest.php b/pkg/sns/Tests/Spec/SnsQueueTest.php
new file mode 100644
index 000000000..39c0e5513
--- /dev/null
+++ b/pkg/sns/Tests/Spec/SnsQueueTest.php
@@ -0,0 +1,14 @@
+createContext();
+
+$queue = $context->createQueue('enqueue');
+$consumer = $context->createConsumer($queue);
+
+while (true) {
+ if ($m = $consumer->receive(20000)) {
+ $consumer->acknowledge($m);
+ echo 'Received message: '.$m->getBody().PHP_EOL;
+ }
+}
+
+echo 'Done'."\n";
diff --git a/pkg/sns/examples/produce.php b/pkg/sns/examples/produce.php
new file mode 100644
index 000000000..d37fce79f
--- /dev/null
+++ b/pkg/sns/examples/produce.php
@@ -0,0 +1,34 @@
+ getenv('ENQUEUE_AWS__SQS__KEY'),
+ 'secret' => getenv('ENQUEUE_AWS__SQS__SECRET'),
+ 'region' => getenv('ENQUEUE_AWS__SQS__REGION'),
+]);
+$context = $factory->createContext();
+
+$topic = $context->createTopic('test_enqueue');
+$context->declareTopic($topic);
+
+$message = $context->createMessage('a_body');
+$message->setProperty('aProp', 'aPropVal');
+$message->setHeader('aHeader', 'aHeaderVal');
+
+$context->createProducer()->send($topic, $message);
diff --git a/pkg/sns/phpunit.xml.dist b/pkg/sns/phpunit.xml.dist
new file mode 100644
index 000000000..3fe611e9c
--- /dev/null
+++ b/pkg/sns/phpunit.xml.dist
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+ ./Tests
+
+
+
+
+
+ .
+
+ ./vendor
+ ./Tests
+
+
+
+
diff --git a/pkg/snsqs/.gitattributes b/pkg/snsqs/.gitattributes
new file mode 100644
index 000000000..bdf2dcb14
--- /dev/null
+++ b/pkg/snsqs/.gitattributes
@@ -0,0 +1,5 @@
+/Tests export-ignore
+.gitattributes export-ignore
+.gitignore export-ignore
+.travis.yml export-ignore
+phpunit.xml.dist export-ignore
diff --git a/pkg/snsqs/.gitignore b/pkg/snsqs/.gitignore
new file mode 100644
index 000000000..a770439e5
--- /dev/null
+++ b/pkg/snsqs/.gitignore
@@ -0,0 +1,6 @@
+*~
+/composer.lock
+/composer.phar
+/phpunit.xml
+/vendor/
+/.idea/
diff --git a/pkg/snsqs/.travis.yml b/pkg/snsqs/.travis.yml
new file mode 100644
index 000000000..9ed4fa123
--- /dev/null
+++ b/pkg/snsqs/.travis.yml
@@ -0,0 +1,21 @@
+sudo: false
+
+git:
+ depth: 10
+
+language: php
+
+php:
+ - '7.1'
+ - '7.2'
+
+cache:
+ directories:
+ - $HOME/.composer/cache
+
+install:
+ - composer self-update
+ - composer install --prefer-source
+
+script:
+ - vendor/bin/phpunit --exclude-group=functional
diff --git a/pkg/snsqs/LICENSE b/pkg/snsqs/LICENSE
new file mode 100644
index 000000000..20211e5fd
--- /dev/null
+++ b/pkg/snsqs/LICENSE
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+Copyright (c) 2018 Max Kotliar
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/pkg/snsqs/README.md b/pkg/snsqs/README.md
new file mode 100644
index 000000000..b944e6c7e
--- /dev/null
+++ b/pkg/snsqs/README.md
@@ -0,0 +1,28 @@
+Supporting Enqueue
+
+Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider:
+
+- [Become a sponsor](https://www.patreon.com/makasim)
+- [Become our client](http://forma-pro.com/)
+
+---
+
+# Amazon SNS-SQS Transport
+
+[](https://gitter.im/php-enqueue/Lobby)
+[](https://travis-ci.org/php-enqueue/snsqs)
+[](https://packagist.org/packages/enqueue/snsqs)
+[](https://packagist.org/packages/enqueue/snsqs)
+
+This is an implementation of Queue Interop specification. It allows you to send and consume message using [Amazon SNS-SQS](https://aws.amazon.com/snsqs/) service.
+
+## Resources
+
+* [Site](https://enqueue.forma-pro.com/)
+* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/snsqs.md)
+* [Questions](https://gitter.im/php-enqueue/Lobby)
+* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
+
+## License
+
+It is released under the [MIT License](LICENSE).
\ No newline at end of file
diff --git a/pkg/snsqs/SnsQsConnectionFactory.php b/pkg/snsqs/SnsQsConnectionFactory.php
new file mode 100644
index 000000000..abb154a9b
--- /dev/null
+++ b/pkg/snsqs/SnsQsConnectionFactory.php
@@ -0,0 +1,117 @@
+ null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
+ * 'region' => null, (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
+ * 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
+ * 'lazy' => true, Enable lazy connection (boolean)
+ * 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
+ * ].
+ *
+ * or
+ *
+ * $config = [
+ * 'sns_key' => null, SNS option
+ * 'sqs_secret' => null, SQS option
+ * 'token' Option for both SNS and SQS
+ * ].
+ *
+ * or
+ *
+ * snsqs:
+ * snsqs:?key=aKey&secret=aSecret&sns_token=aSnsToken&sqs_token=aSqsToken
+ *
+ * @param array|string|null $config
+ */
+ public function __construct($config = 'snsqs:')
+ {
+ if (empty($config)) {
+ $this->snsConfig = [];
+ $this->sqsConfig = [];
+ } elseif (is_string($config)) {
+ $this->parseDsn($config);
+ } elseif (is_array($config)) {
+ if (array_key_exists('dsn', $config)) {
+ $this->parseDsn($config['dsn']);
+ } else {
+ $this->parseOptions($config);
+ }
+ } else {
+ throw new \LogicException('The config must be either an array of options, a DSN string or null');
+ }
+ }
+
+ /**
+ * @return SnsQsContext
+ */
+ public function createContext(): Context
+ {
+ return new SnsQsContext(function () {
+ return (new SnsConnectionFactory($this->snsConfig))->createContext();
+ }, function () {
+ return (new SqsConnectionFactory($this->sqsConfig))->createContext();
+ });
+ }
+
+ private function parseDsn(string $dsn): void
+ {
+ $dsn = Dsn::parseFirst($dsn);
+
+ if ('snsqs' !== $dsn->getSchemeProtocol()) {
+ throw new \LogicException(sprintf(
+ 'The given scheme protocol "%s" is not supported. It must be "snsqs"',
+ $dsn->getSchemeProtocol()
+ ));
+ }
+
+ $this->parseOptions($dsn->getQuery());
+ }
+
+ private function parseOptions(array $options): void
+ {
+ // set default options
+ foreach ($options as $key => $value) {
+ if (false === in_array(substr($key, 0, 4), ['sns_', 'sqs_'], true)) {
+ $this->snsConfig[$key] = $value;
+ $this->sqsConfig[$key] = $value;
+ }
+ }
+
+ // set transport specific options
+ foreach ($options as $key => $value) {
+ switch (substr($key, 0, 4)) {
+ case 'sns_':
+ $this->snsConfig[substr($key, 4)] = $value;
+ break;
+ case 'sqs_':
+ $this->sqsConfig[substr($key, 4)] = $value;
+ break;
+ }
+ }
+ }
+}
diff --git a/pkg/snsqs/SnsQsConsumer.php b/pkg/snsqs/SnsQsConsumer.php
new file mode 100644
index 000000000..82209896e
--- /dev/null
+++ b/pkg/snsqs/SnsQsConsumer.php
@@ -0,0 +1,114 @@
+context = $context;
+ $this->consumer = $consumer;
+ $this->queue = $queue;
+ }
+
+ public function getQueue(): Queue
+ {
+ return $this->queue;
+ }
+
+ public function receive(int $timeout = 0): ?Message
+ {
+ if ($sqsMessage = $this->consumer->receive($timeout)) {
+ return $this->convertMessage($sqsMessage);
+ }
+
+ return null;
+ }
+
+ public function receiveNoWait(): ?Message
+ {
+ if ($sqsMessage = $this->consumer->receiveNoWait()) {
+ return $this->convertMessage($sqsMessage);
+ }
+
+ return null;
+ }
+
+ /**
+ * @param SnsQsMessage $message
+ */
+ public function acknowledge(Message $message): void
+ {
+ InvalidMessageException::assertMessageInstanceOf($message, SnsQsMessage::class);
+
+ $this->consumer->acknowledge($message->getSqsMessage());
+ }
+
+ /**
+ * @param SnsQsMessage $message
+ */
+ public function reject(Message $message, bool $requeue = false): void
+ {
+ InvalidMessageException::assertMessageInstanceOf($message, SnsQsMessage::class);
+
+ $this->consumer->reject($message->getSqsMessage(), $requeue);
+ }
+
+ private function convertMessage(SqsMessage $sqsMessage): SnsQsMessage
+ {
+ $message = $this->context->createMessage();
+ $message->setRedelivered($sqsMessage->isRedelivered());
+ $message->setSqsMessage($sqsMessage);
+
+ $body = $sqsMessage->getBody();
+
+ if (isset($body[0]) && '{' === $body[0]) {
+ $data = json_decode($sqsMessage->getBody(), true);
+
+ if (isset($data['TopicArn']) && isset($data['Type']) && 'Notification' === $data['Type']) {
+ // SNS message conversion
+ if (isset($data['Message'])) {
+ $message->setBody((string) $data['Message']);
+ }
+
+ if (isset($data['MessageAttributes']['Headers'])) {
+ $headersData = json_decode($data['MessageAttributes']['Headers']['Value'], true);
+
+ $message->setHeaders($headersData[0]);
+ $message->setProperties($headersData[1]);
+ }
+
+ return $message;
+ }
+ }
+
+ $message->setBody($sqsMessage->getBody());
+ $message->setProperties($sqsMessage->getProperties());
+
+ return $message;
+ }
+}
diff --git a/pkg/snsqs/SnsQsContext.php b/pkg/snsqs/SnsQsContext.php
new file mode 100644
index 000000000..d520cdb10
--- /dev/null
+++ b/pkg/snsqs/SnsQsContext.php
@@ -0,0 +1,214 @@
+snsContext = $snsContext;
+ } elseif (is_callable($snsContext)) {
+ $this->snsContextFactory = $snsContext;
+ } else {
+ throw new \InvalidArgumentException(sprintf(
+ 'The $snsContext argument must be either %s or callable that returns %s once called.',
+ SnsContext::class,
+ SnsContext::class
+ ));
+ }
+
+ if ($sqsContext instanceof SqsContext) {
+ $this->sqsContext = $sqsContext;
+ } elseif (is_callable($sqsContext)) {
+ $this->sqsContextFactory = $sqsContext;
+ } else {
+ throw new \InvalidArgumentException(sprintf(
+ 'The $sqsContext argument must be either %s or callable that returns %s once called.',
+ SqsContext::class,
+ SqsContext::class
+ ));
+ }
+ }
+
+ /**
+ * @return SnsQsMessage
+ */
+ public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
+ {
+ return new SnsQsMessage($body, $properties, $headers);
+ }
+
+ /**
+ * @return SnsQsTopic
+ */
+ public function createTopic(string $topicName): Topic
+ {
+ return new SnsQsTopic($topicName);
+ }
+
+ /**
+ * @return SnsQsQueue
+ */
+ public function createQueue(string $queueName): Queue
+ {
+ return new SnsQsQueue($queueName);
+ }
+
+ public function createTemporaryQueue(): Queue
+ {
+ throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function createProducer(): Producer
+ {
+ return new SnsQsProducer($this->getSnsContext(), $this->getSqsContext());
+ }
+
+ /**
+ * @param SnsQsQueue $destination
+ */
+ public function createConsumer(Destination $destination): Consumer
+ {
+ InvalidDestinationException::assertDestinationInstanceOf($destination, SnsQsQueue::class);
+
+ return new SnsQsConsumer($this, $this->getSqsContext()->createConsumer($destination), $destination);
+ }
+
+ /**
+ * @param SnsQsQueue $queue
+ */
+ public function purgeQueue(Queue $queue): void
+ {
+ InvalidDestinationException::assertDestinationInstanceOf($queue, SnsQsQueue::class);
+
+ $this->getSqsContext()->purgeQueue($queue);
+ }
+
+ public function createSubscriptionConsumer(): SubscriptionConsumer
+ {
+ throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
+ }
+
+ public function declareTopic(SnsQsTopic $topic): void
+ {
+ $this->getSnsContext()->declareTopic($topic);
+ }
+
+ public function deleteTopic(SnsQsTopic $topic): void
+ {
+ $this->getSnsContext()->deleteTopic($topic);
+ }
+
+ public function declareQueue(SnsQsQueue $queue): void
+ {
+ $this->getSqsContext()->declareQueue($queue);
+ }
+
+ public function deleteQueue(SnsQsQueue $queue): void
+ {
+ $this->getSqsContext()->deleteQueue($queue);
+ }
+
+ public function bind(SnsQsTopic $topic, SnsQsQueue $queue): void
+ {
+ $this->getSnsContext()->subscribe(new SnsSubscribe(
+ $topic,
+ $this->getSqsContext()->getQueueArn($queue),
+ SnsSubscribe::PROTOCOL_SQS
+ ));
+ }
+
+ public function unbind(SnsQsTopic $topic, SnsQsQueue $queue): void
+ {
+ $this->getSnsContext()->unsubscibe(new SnsUnsubscribe(
+ $topic,
+ $this->getSqsContext()->getQueueArn($queue),
+ SnsSubscribe::PROTOCOL_SQS
+ ));
+ }
+
+ public function close(): void
+ {
+ $this->getSnsContext()->close();
+ $this->getSqsContext()->close();
+ }
+
+ private function getSnsContext(): SnsContext
+ {
+ if (null === $this->snsContext) {
+ $context = call_user_func($this->snsContextFactory);
+ if (false == $context instanceof SnsContext) {
+ throw new \LogicException(sprintf(
+ 'The factory must return instance of %s. It returned %s',
+ SnsContext::class,
+ is_object($context) ? get_class($context) : gettype($context)
+ ));
+ }
+
+ $this->snsContext = $context;
+ }
+
+ return $this->snsContext;
+ }
+
+ private function getSqsContext(): SqsContext
+ {
+ if (null === $this->sqsContext) {
+ $context = call_user_func($this->sqsContextFactory);
+ if (false == $context instanceof SqsContext) {
+ throw new \LogicException(sprintf(
+ 'The factory must return instance of %s. It returned %s',
+ SqsContext::class,
+ is_object($context) ? get_class($context) : gettype($context)
+ ));
+ }
+
+ $this->sqsContext = $context;
+ }
+
+ return $this->sqsContext;
+ }
+}
diff --git a/pkg/snsqs/SnsQsMessage.php b/pkg/snsqs/SnsQsMessage.php
new file mode 100644
index 000000000..e34a103ff
--- /dev/null
+++ b/pkg/snsqs/SnsQsMessage.php
@@ -0,0 +1,37 @@
+body = $body;
+ $this->properties = $properties;
+ $this->headers = $headers;
+ $this->redelivered = false;
+ }
+
+ public function setSqsMessage(SqsMessage $message): void
+ {
+ $this->sqsMessage = $message;
+ }
+
+ public function getSqsMessage(): SqsMessage
+ {
+ return $this->sqsMessage;
+ }
+}
diff --git a/pkg/snsqs/SnsQsProducer.php b/pkg/snsqs/SnsQsProducer.php
new file mode 100644
index 000000000..1baeef307
--- /dev/null
+++ b/pkg/snsqs/SnsQsProducer.php
@@ -0,0 +1,136 @@
+snsContext = $snsContext;
+ $this->sqsContext = $sqsContext;
+ }
+
+ /**
+ * @param SnsQsTopic $destination
+ * @param SnsQsMessage $message
+ */
+ public function send(Destination $destination, Message $message): void
+ {
+ InvalidMessageException::assertMessageInstanceOf($message, SnsQsMessage::class);
+
+ if (false == $destination instanceof SnsQsTopic && false == $destination instanceof SnsQsQueue) {
+ throw new InvalidDestinationException(sprintf(
+ 'The destination must be an instance of [%s|%s] but got %s.',
+ SnsQsTopic::class, SnsQsQueue::class,
+ is_object($destination) ? get_class($destination) : gettype($destination)
+ ));
+ }
+
+ if ($destination instanceof SnsQsTopic) {
+ $snsMessage = $this->snsContext->createMessage(
+ $message->getBody(),
+ $message->getProperties(),
+ $message->getHeaders()
+ );
+
+ $this->getSnsProducer()->send($destination, $snsMessage);
+ } else {
+ $sqsMessage = $this->sqsContext->createMessage(
+ $message->getBody(),
+ $message->getProperties(),
+ $message->getHeaders()
+ );
+
+ $this->getSqsProducer()->send($destination, $sqsMessage);
+ }
+ }
+
+ public function setDeliveryDelay(int $deliveryDelay = null): Producer
+ {
+ $this->getSnsProducer()->setDeliveryDelay($deliveryDelay);
+ $this->getSqsProducer()->setDeliveryDelay($deliveryDelay);
+
+ return $this;
+ }
+
+ public function getDeliveryDelay(): ?int
+ {
+ return $this->getSnsProducer()->getDeliveryDelay();
+ }
+
+ public function setPriority(int $priority = null): Producer
+ {
+ $this->getSnsProducer()->setPriority($priority);
+ $this->getSqsProducer()->setPriority($priority);
+
+ return $this;
+ }
+
+ public function getPriority(): ?int
+ {
+ return $this->getSnsProducer()->getPriority();
+ }
+
+ public function setTimeToLive(int $timeToLive = null): Producer
+ {
+ $this->getSnsProducer()->setTimeToLive($timeToLive);
+ $this->getSqsProducer()->setTimeToLive($timeToLive);
+
+ return $this;
+ }
+
+ public function getTimeToLive(): ?int
+ {
+ return $this->getSnsProducer()->getTimeToLive();
+ }
+
+ private function getSnsProducer(): SnsProducer
+ {
+ if (null === $this->snsProducer) {
+ $this->snsProducer = $this->snsContext->createProducer();
+ }
+
+ return $this->snsProducer;
+ }
+
+ private function getSqsProducer(): SqsProducer
+ {
+ if (null === $this->sqsProducer) {
+ $this->sqsProducer = $this->sqsContext->createProducer();
+ }
+
+ return $this->sqsProducer;
+ }
+}
diff --git a/pkg/snsqs/SnsQsQueue.php b/pkg/snsqs/SnsQsQueue.php
new file mode 100644
index 000000000..92c3a542b
--- /dev/null
+++ b/pkg/snsqs/SnsQsQueue.php
@@ -0,0 +1,11 @@
+createMock(SqsContext::class);
+ $sqsContext
+ ->expects($this->any())
+ ->method('createConsumer')
+ ->willReturn($this->createMock(SqsConsumer::class))
+ ;
+
+ return new SnsQsContext(
+ $this->createMock(SnsContext::class),
+ $sqsContext
+ );
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsFactoryTrait.php b/pkg/snsqs/Tests/Spec/SnsQsFactoryTrait.php
new file mode 100644
index 000000000..efc4a7046
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsFactoryTrait.php
@@ -0,0 +1,68 @@
+snsQsContext = $this->buildSnsQsContext();
+ }
+
+ protected function createSnsQsQueue(string $queueName): SnsQsQueue
+ {
+ $queueName = $queueName.time();
+
+ $this->snsQsQueue = $this->snsQsContext->createQueue($queueName);
+ $this->snsQsContext->declareQueue($this->snsQsQueue);
+
+ if ($this->snsQsTopic) {
+ $this->snsQsContext->bind($this->snsQsTopic, $this->snsQsQueue);
+ }
+
+ return $this->snsQsQueue;
+ }
+
+ protected function createSnsQsTopic(string $topicName): SnsQsTopic
+ {
+ $topicName = $topicName.time();
+
+ $this->snsQsTopic = $this->snsQsContext->createTopic($topicName);
+ $this->snsQsContext->declareTopic($this->snsQsTopic);
+
+ return $this->snsQsTopic;
+ }
+
+ protected function cleanUpSnsQs(): void
+ {
+ if ($this->snsQsTopic) {
+ $this->snsQsContext->deleteTopic($this->snsQsTopic);
+ }
+
+ if ($this->snsQsQueue) {
+ $this->snsQsContext->deleteQueue($this->snsQsQueue);
+ }
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsMessageTest.php b/pkg/snsqs/Tests/Spec/SnsQsMessageTest.php
new file mode 100644
index 000000000..a2815cde5
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsMessageTest.php
@@ -0,0 +1,14 @@
+createMock(SnsContext::class),
+ $this->createMock(SqsContext::class)
+ );
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsQueueTest.php b/pkg/snsqs/Tests/Spec/SnsQsQueueTest.php
new file mode 100644
index 000000000..6a6bd4dfd
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsQueueTest.php
@@ -0,0 +1,14 @@
+cleanUpSnsQs();
+ }
+
+ protected function createContext()
+ {
+ return $this->createSnsQsContext();
+ }
+
+ protected function createQueue(Context $context, $queueName)
+ {
+ return $this->createSnsQsQueue($queueName);
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsSendToAndReceiveNoWaitFromQueueTest.php b/pkg/snsqs/Tests/Spec/SnsQsSendToAndReceiveNoWaitFromQueueTest.php
new file mode 100644
index 000000000..72e29eeae
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsSendToAndReceiveNoWaitFromQueueTest.php
@@ -0,0 +1,34 @@
+cleanUpSnsQs();
+ }
+
+ protected function createContext()
+ {
+ return $this->createSnsQsContext();
+ }
+
+ protected function createQueue(Context $context, $queueName)
+ {
+ return $this->createSnsQsQueue($queueName);
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveFromQueueSpec.php b/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveFromQueueSpec.php
new file mode 100644
index 000000000..f32e37a94
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveFromQueueSpec.php
@@ -0,0 +1,39 @@
+cleanUpSnsQs();
+ }
+
+ protected function createContext()
+ {
+ return $this->createSnsQsContext();
+ }
+
+ protected function createTopic(Context $context, $topicName)
+ {
+ return $this->createSnsQsTopic($topicName);
+ }
+
+ protected function createQueue(Context $context, $queueName)
+ {
+ return $this->createSnsQsQueue($queueName);
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveNoWaitFromQueueTest.php b/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveNoWaitFromQueueTest.php
new file mode 100644
index 000000000..5d404e4a3
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsSendToTopicAndReceiveNoWaitFromQueueTest.php
@@ -0,0 +1,39 @@
+cleanUpSnsQs();
+ }
+
+ protected function createContext()
+ {
+ return $this->createSnsQsContext();
+ }
+
+ protected function createTopic(Context $context, $topicName)
+ {
+ return $this->createSnsQsTopic($topicName);
+ }
+
+ protected function createQueue(Context $context, $queueName)
+ {
+ return $this->createSnsQsQueue($queueName);
+ }
+}
diff --git a/pkg/snsqs/Tests/Spec/SnsQsTopicTest.php b/pkg/snsqs/Tests/Spec/SnsQsTopicTest.php
new file mode 100644
index 000000000..94a455987
--- /dev/null
+++ b/pkg/snsqs/Tests/Spec/SnsQsTopicTest.php
@@ -0,0 +1,14 @@
+ getenv('SNS_DSN'),
+ 'sqs' => getenv('SQS_DSN'),
+]))->createContext();
+
+$topic = $context->createTopic('topic');
+$queue = $context->createQueue('queue');
+
+$context->declareTopic($topic);
+$context->declareQueue($queue);
+$context->bind($topic, $queue);
+
+$consumer = $context->createConsumer($queue);
+
+while (true) {
+ if ($m = $consumer->receive(20000)) {
+ $consumer->acknowledge($m);
+ echo 'Received message: '.$m->getBody().' '.json_encode($m->getHeaders()).' '.json_encode($m->getProperties()).PHP_EOL;
+ }
+}
+echo 'Done'."\n";
diff --git a/pkg/snsqs/examples/produce.php b/pkg/snsqs/examples/produce.php
new file mode 100644
index 000000000..0396d9cd0
--- /dev/null
+++ b/pkg/snsqs/examples/produce.php
@@ -0,0 +1,40 @@
+ getenv('SNS_DSN'),
+ 'sqs' => getenv('SQS_DSN'),
+]))->createContext();
+
+$topic = $context->createTopic('topic');
+$queue = $context->createQueue('queue');
+
+$context->declareTopic($topic);
+$context->declareQueue($queue);
+$context->bind($topic, $queue);
+
+$message = $context->createMessage('Hello Bar!', ['key' => 'value'], ['key2' => 'value2']);
+
+while (true) {
+ $context->createProducer()->send($topic, $message);
+ echo 'Sent message: '.$message->getBody().PHP_EOL;
+ sleep(1);
+}
+
+echo 'Done'."\n";
diff --git a/pkg/snsqs/phpunit.xml.dist b/pkg/snsqs/phpunit.xml.dist
new file mode 100644
index 000000000..e9b3b30e8
--- /dev/null
+++ b/pkg/snsqs/phpunit.xml.dist
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+ ./Tests
+
+
+
+
+
+ .
+
+ ./vendor
+ ./Tests
+
+
+
+
diff --git a/pkg/sqs/README.md b/pkg/sqs/README.md
index 7c46d801c..1c7d7da08 100644
--- a/pkg/sqs/README.md
+++ b/pkg/sqs/README.md
@@ -14,12 +14,12 @@ Enqueue is an MIT-licensed open source project with its ongoing development made
[](https://packagist.org/packages/enqueue/sqs)
[](https://packagist.org/packages/enqueue/sqs)
-This is an implementation of Queue Interop specification. It allows you to send and consume message through Amazon SQS library.
+This is an implementation of Queue Interop specification. It allows you to send and consume message using [Amazon SQS](https://aws.amazon.com/sqs/) service.
## Resources
* [Site](https://enqueue.forma-pro.com/)
-* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
+* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/sqs.md)
* [Questions](https://gitter.im/php-enqueue/Lobby)
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
diff --git a/pkg/sqs/SqsClient.php b/pkg/sqs/SqsClient.php
index 4fc87fabb..0d61584bf 100644
--- a/pkg/sqs/SqsClient.php
+++ b/pkg/sqs/SqsClient.php
@@ -26,7 +26,7 @@ class SqsClient
private $inputClient;
/**
- * @param AwsSqsClient|callable $inputClient
+ * @param AwsSqsClient|MultiRegionClient|callable $inputClient
*/
public function __construct($inputClient)
{
@@ -58,6 +58,11 @@ public function getQueueUrl(array $args): Result
return $this->callApi('getQueueUrl', $args);
}
+ public function getQueueAttributes(array $args): Result
+ {
+ return $this->callApi('getQueueAttributes', $args);
+ }
+
public function createQueue(array $args): Result
{
return $this->callApi('createQueue', $args);
diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php
index 4a99c752e..1a8a967c7 100644
--- a/pkg/sqs/SqsConnectionFactory.php
+++ b/pkg/sqs/SqsConnectionFactory.php
@@ -40,7 +40,7 @@ class SqsConnectionFactory implements ConnectionFactory
* sqs:
* sqs::?key=aKey&secret=aSecret&token=aToken
*
- * @param array|string|SqsClient|null $config
+ * @param array|string|AwsSqsClient|null $config
*/
public function __construct($config = 'sqs:')
{
diff --git a/pkg/sqs/SqsContext.php b/pkg/sqs/SqsContext.php
index 8be6fcddb..60906f72f 100644
--- a/pkg/sqs/SqsContext.php
+++ b/pkg/sqs/SqsContext.php
@@ -29,6 +29,11 @@ class SqsContext implements Context
*/
private $queueUrls;
+ /**
+ * @var array
+ */
+ private $queueArns;
+
/**
* @var array
*/
@@ -38,6 +43,9 @@ public function __construct(SqsClient $client, array $config)
{
$this->client = $client;
$this->config = $config;
+
+ $this->queueUrls = [];
+ $this->queueArns = [];
}
/**
@@ -157,6 +165,27 @@ public function getQueueUrl(SqsDestination $destination): string
return $this->queueUrls[$destination->getQueueName()] = (string) $result->get('QueueUrl');
}
+ public function getQueueArn(SqsDestination $destination): string
+ {
+ if (isset($this->queueArns[$destination->getQueueName()])) {
+ return $this->queueArns[$destination->getQueueName()];
+ }
+
+ $arguments = [
+ '@region' => $destination->getRegion(),
+ 'QueueUrl' => $this->getQueueUrl($destination),
+ 'AttributeNames' => ['QueueArn'],
+ ];
+
+ $result = $this->client->getQueueAttributes($arguments);
+
+ if (false == $arn = $result->search('Attributes.QueueArn')) {
+ throw new \RuntimeException(sprintf('QueueArn cannot be resolved. queueName: "%s"', $destination->getQueueName()));
+ }
+
+ return $this->queueArns[$destination->getQueueName()] = (string) $arn;
+ }
+
public function declareQueue(SqsDestination $dest): void
{
$result = $this->client->createQueue([
diff --git a/pkg/sqs/Tests/SqsClientTest.php b/pkg/sqs/Tests/SqsClientTest.php
index 2a45edc72..08c44a307 100644
--- a/pkg/sqs/Tests/SqsClientTest.php
+++ b/pkg/sqs/Tests/SqsClientTest.php
@@ -223,6 +223,13 @@ public function provideApiCallsSingleClient()
AwsSqsClient::class,
];
+ yield [
+ 'getQueueAttributes',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ AwsSqsClient::class,
+ ];
+
yield [
'createQueue',
['fooArg' => 'fooArgVal'],
@@ -275,6 +282,13 @@ public function provideApiCallsMultipleClient()
MultiRegionClient::class,
];
+ yield [
+ 'getQueueAttributes',
+ ['fooArg' => 'fooArgVal'],
+ ['bar' => 'barVal'],
+ MultiRegionClient::class,
+ ];
+
yield [
'createQueue',
['fooArg' => 'fooArgVal'],
diff --git a/pkg/sqs/Tests/SqsContextTest.php b/pkg/sqs/Tests/SqsContextTest.php
index 53dfc4358..1f5fcf239 100644
--- a/pkg/sqs/Tests/SqsContextTest.php
+++ b/pkg/sqs/Tests/SqsContextTest.php
@@ -308,6 +308,41 @@ public function testShouldAllowGetQueueUrl()
$context->getQueueUrl(new SqsDestination('aQueueName'));
}
+ public function testShouldAllowGetQueueArn()
+ {
+ $sqsClient = $this->createSqsClientMock();
+ $sqsClient
+ ->expects($this->once())
+ ->method('getQueueUrl')
+ ->with($this->identicalTo([
+ '@region' => 'theRegion',
+ 'QueueName' => 'aQueueName',
+ ]))
+ ->willReturn(new Result(['QueueUrl' => 'theQueueUrl']))
+ ;
+ $sqsClient
+ ->expects($this->once())
+ ->method('getQueueAttributes')
+ ->with($this->identicalTo([
+ '@region' => 'theRegion',
+ 'QueueUrl' => 'theQueueUrl',
+ 'AttributeNames' => ['QueueArn'],
+ ]))
+ ->willReturn(new Result([
+ 'Attributes' => [
+ 'QueueArn' => 'theQueueArn',
+ ],
+ ]))
+ ;
+
+ $context = new SqsContext($sqsClient, []);
+
+ $queue = $context->createQueue('aQueueName');
+ $queue->setRegion('theRegion');
+
+ $this->assertSame('theQueueArn', $context->getQueueArn($queue));
+ }
+
public function testShouldAllowGetQueueUrlWithCustomRegion()
{
$sqsClient = $this->createSqsClientMock();
diff --git a/pkg/test/SnsExtension.php b/pkg/test/SnsExtension.php
new file mode 100644
index 000000000..6c01abe2f
--- /dev/null
+++ b/pkg/test/SnsExtension.php
@@ -0,0 +1,18 @@
+createContext();
+ }
+}
diff --git a/pkg/test/SnsQsExtension.php b/pkg/test/SnsQsExtension.php
new file mode 100644
index 000000000..719f5ce58
--- /dev/null
+++ b/pkg/test/SnsQsExtension.php
@@ -0,0 +1,18 @@
+createContext();
+ }
+}