Skip to content

Commit 1e11dce

Browse files
committed
fix(mongodb): Exception throwing fatal error, Broken handling of MongodbConnectionFactory config, Incorrect MongodbConnectionFactory documentation
Closes: php-enqueue#1031, php-enqueue#1027
1 parent 1dedaa4 commit 1e11dce

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Diff for: pkg/mongodb/MongodbConnectionFactory.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MongodbConnectionFactory implements ConnectionFactory
2727
*
2828
* or
2929
*
30-
* mongodb://127.0.0.1:27017/dbname?polling_interval=1000&enqueue_collection=enqueue
30+
* mongodb://127.0.0.1:27017/defaultauthdb?polling_interval=1000&enqueue_database=enqueue&enqueue_collection=enqueue
3131
*
3232
* @param array|string|null $config
3333
*/
@@ -38,7 +38,10 @@ public function __construct($config = 'mongodb:')
3838
} elseif (is_string($config)) {
3939
$config = $this->parseDsn($config);
4040
} elseif (is_array($config)) {
41-
$config = $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']);
41+
$config = array_replace(
42+
$config,
43+
$this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn'])
44+
);
4245
} else {
4346
throw new \LogicException('The config must be either an array of options, a DSN string or null');
4447
}
@@ -86,6 +89,8 @@ public static function parseDsn(string $dsn): array
8689
];
8790
}
8891
$config['dsn'] = $dsn;
92+
// FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break.
93+
// see: https://github.com/php-enqueue/enqueue-dev/issues/1027
8994
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
9095
$pathParts = explode('/', $parsedUrl['path']);
9196
//DB name
@@ -103,6 +108,9 @@ public static function parseDsn(string $dsn): array
103108
if (!empty($queryParts['enqueue_collection'])) {
104109
$config['collection_name'] = $queryParts['enqueue_collection'];
105110
}
111+
if (!empty($queryParts['enqueue_database'])) {
112+
$config['dbname'] = $queryParts['enqueue_database'];
113+
}
106114
}
107115

108116
return $config;

Diff for: pkg/mongodb/MongodbProducer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function send(Destination $destination, Message $message): void
110110
$collection = $this->context->getCollection();
111111
$collection->insertOne($mongoMessage);
112112
} catch (\Exception $e) {
113-
throw new Exception('The transport has failed to send the message due to some internal error.', null, $e);
113+
throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e);
114114
}
115115
}
116116

0 commit comments

Comments
 (0)