Skip to content

Commit 749fb74

Browse files
authored
Merge pull request #1032 from josefsabl/master
fix(mongodb): Exception throwing fatal error, Broken handling of Mong…
2 parents 1dedaa4 + 54fe9e6 commit 749fb74

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

Diff for: pkg/mongodb/MongodbConnectionFactory.php

+11-7
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
}
@@ -74,18 +77,16 @@ public static function parseDsn(string $dsn): array
7477
'mongodb' => true,
7578
];
7679
if (false == isset($parsedUrl['scheme'])) {
77-
throw new \LogicException(sprintf(
78-
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
79-
$parsedUrl['scheme'],
80-
implode('", "', array_keys($supported))
81-
));
80+
throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedUrl['scheme'], implode('", "', array_keys($supported))));
8281
}
8382
if ('mongodb:' === $dsn) {
8483
return [
8584
'dsn' => 'mongodb://127.0.0.1/',
8685
];
8786
}
8887
$config['dsn'] = $dsn;
88+
// FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break.
89+
// see: https://github.com/php-enqueue/enqueue-dev/issues/1027
8990
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
9091
$pathParts = explode('/', $parsedUrl['path']);
9192
//DB name
@@ -103,6 +104,9 @@ public static function parseDsn(string $dsn): array
103104
if (!empty($queryParts['enqueue_collection'])) {
104105
$config['collection_name'] = $queryParts['enqueue_collection'];
105106
}
107+
if (!empty($queryParts['enqueue_database'])) {
108+
$config['dbname'] = $queryParts['enqueue_database'];
109+
}
106110
}
107111

108112
return $config;

Diff for: pkg/mongodb/MongodbProducer.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ public function send(Destination $destination, Message $message): void
7777
$delay = $message->getDeliveryDelay();
7878
if ($delay) {
7979
if (!is_int($delay)) {
80-
throw new \LogicException(sprintf(
81-
'Delay must be integer but got: "%s"',
82-
is_object($delay) ? get_class($delay) : gettype($delay)
83-
));
80+
throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay)));
8481
}
8582

8683
if ($delay <= 0) {
@@ -93,10 +90,7 @@ public function send(Destination $destination, Message $message): void
9390
$timeToLive = $message->getTimeToLive();
9491
if ($timeToLive) {
9592
if (!is_int($timeToLive)) {
96-
throw new \LogicException(sprintf(
97-
'TimeToLive must be integer but got: "%s"',
98-
is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)
99-
));
93+
throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)));
10094
}
10195

10296
if ($timeToLive <= 0) {
@@ -110,7 +104,7 @@ public function send(Destination $destination, Message $message): void
110104
$collection = $this->context->getCollection();
111105
$collection->insertOne($mongoMessage);
112106
} catch (\Exception $e) {
113-
throw new Exception('The transport has failed to send the message due to some internal error.', null, $e);
107+
throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e);
114108
}
115109
}
116110

0 commit comments

Comments
 (0)