From 1e11dced08315dc1b8bb7786bcdd1541c0ee3626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20S=C3=A1bl?= Date: Wed, 18 Mar 2020 10:53:14 +0100 Subject: [PATCH 1/2] fix(mongodb): Exception throwing fatal error, Broken handling of MongodbConnectionFactory config, Incorrect MongodbConnectionFactory documentation Closes: #1031, #1027 --- pkg/mongodb/MongodbConnectionFactory.php | 12 ++++++++++-- pkg/mongodb/MongodbProducer.php | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/mongodb/MongodbConnectionFactory.php b/pkg/mongodb/MongodbConnectionFactory.php index a5148f1c0..05fdd89ac 100644 --- a/pkg/mongodb/MongodbConnectionFactory.php +++ b/pkg/mongodb/MongodbConnectionFactory.php @@ -27,7 +27,7 @@ class MongodbConnectionFactory implements ConnectionFactory * * or * - * mongodb://127.0.0.1:27017/dbname?polling_interval=1000&enqueue_collection=enqueue + * mongodb://127.0.0.1:27017/defaultauthdb?polling_interval=1000&enqueue_database=enqueue&enqueue_collection=enqueue * * @param array|string|null $config */ @@ -38,7 +38,10 @@ public function __construct($config = 'mongodb:') } elseif (is_string($config)) { $config = $this->parseDsn($config); } elseif (is_array($config)) { - $config = $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']); + $config = array_replace( + $config, + $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']) + ); } else { throw new \LogicException('The config must be either an array of options, a DSN string or null'); } @@ -86,6 +89,8 @@ public static function parseDsn(string $dsn): array ]; } $config['dsn'] = $dsn; + // FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break. + // see: https://github.com/php-enqueue/enqueue-dev/issues/1027 if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) { $pathParts = explode('/', $parsedUrl['path']); //DB name @@ -103,6 +108,9 @@ public static function parseDsn(string $dsn): array if (!empty($queryParts['enqueue_collection'])) { $config['collection_name'] = $queryParts['enqueue_collection']; } + if (!empty($queryParts['enqueue_database'])) { + $config['dbname'] = $queryParts['enqueue_database']; + } } return $config; diff --git a/pkg/mongodb/MongodbProducer.php b/pkg/mongodb/MongodbProducer.php index d27f014eb..12956f714 100644 --- a/pkg/mongodb/MongodbProducer.php +++ b/pkg/mongodb/MongodbProducer.php @@ -110,7 +110,7 @@ public function send(Destination $destination, Message $message): void $collection = $this->context->getCollection(); $collection->insertOne($mongoMessage); } catch (\Exception $e) { - throw new Exception('The transport has failed to send the message due to some internal error.', null, $e); + throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e); } } From 54fe9e6ce88f0d9b5bdc6df65b564f1b08d7fbe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20S=C3=A1bl?= Date: Thu, 19 Mar 2020 11:33:37 +0100 Subject: [PATCH 2/2] cs(mongodb): Fix legacy code for new coding standards --- pkg/mongodb/MongodbConnectionFactory.php | 6 +----- pkg/mongodb/MongodbProducer.php | 10 ++-------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/pkg/mongodb/MongodbConnectionFactory.php b/pkg/mongodb/MongodbConnectionFactory.php index 05fdd89ac..3d7624904 100644 --- a/pkg/mongodb/MongodbConnectionFactory.php +++ b/pkg/mongodb/MongodbConnectionFactory.php @@ -77,11 +77,7 @@ public static function parseDsn(string $dsn): array 'mongodb' => true, ]; if (false == isset($parsedUrl['scheme'])) { - throw new \LogicException(sprintf( - 'The given DSN schema "%s" is not supported. There are supported schemes: "%s".', - $parsedUrl['scheme'], - implode('", "', array_keys($supported)) - )); + throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedUrl['scheme'], implode('", "', array_keys($supported)))); } if ('mongodb:' === $dsn) { return [ diff --git a/pkg/mongodb/MongodbProducer.php b/pkg/mongodb/MongodbProducer.php index 12956f714..d26696cf8 100644 --- a/pkg/mongodb/MongodbProducer.php +++ b/pkg/mongodb/MongodbProducer.php @@ -77,10 +77,7 @@ public function send(Destination $destination, Message $message): void $delay = $message->getDeliveryDelay(); if ($delay) { if (!is_int($delay)) { - throw new \LogicException(sprintf( - 'Delay must be integer but got: "%s"', - is_object($delay) ? get_class($delay) : gettype($delay) - )); + throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay))); } if ($delay <= 0) { @@ -93,10 +90,7 @@ public function send(Destination $destination, Message $message): void $timeToLive = $message->getTimeToLive(); if ($timeToLive) { if (!is_int($timeToLive)) { - throw new \LogicException(sprintf( - 'TimeToLive must be integer but got: "%s"', - is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive) - )); + throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive))); } if ($timeToLive <= 0) {