From 895dcc73d08b2b6ae206860dad1da968b9199a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 25 Jul 2024 11:20:57 +0200 Subject: [PATCH] PHPORM-222 Register the `BusServiceProvider` when `BatchRepository` is built (#3071) --- src/MongoDBBusServiceProvider.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/MongoDBBusServiceProvider.php b/src/MongoDBBusServiceProvider.php index c77ccd118..d3d6f25fc 100644 --- a/src/MongoDBBusServiceProvider.php +++ b/src/MongoDBBusServiceProvider.php @@ -8,8 +8,11 @@ use Illuminate\Container\Container; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; +use InvalidArgumentException; use MongoDB\Laravel\Bus\MongoBatchRepository; +use function sprintf; + class MongoDBBusServiceProvider extends ServiceProvider implements DeferrableProvider { /** @@ -18,14 +21,21 @@ class MongoDBBusServiceProvider extends ServiceProvider implements DeferrablePro public function register() { $this->app->singleton(MongoBatchRepository::class, function (Container $app) { + $connection = $app->make('db')->connection($app->config->get('queue.batching.database')); + + if (! $connection instanceof Connection) { + throw new InvalidArgumentException(sprintf('The "mongodb" batch driver requires a MongoDB connection. The "%s" connection uses the "%s" driver.', $connection->getName(), $connection->getDriverName())); + } + return new MongoBatchRepository( $app->make(BatchFactory::class), - $app->make('db')->connection($app->config->get('queue.batching.database')), + $connection, $app->config->get('queue.batching.collection', 'job_batches'), ); }); - /** @see BusServiceProvider::registerBatchServices() */ + /** The {@see BatchRepository} service is registered in {@see BusServiceProvider} */ + $this->app->register(BusServiceProvider::class); $this->app->extend(BatchRepository::class, function (BatchRepository $repository, Container $app) { $driver = $app->config->get('queue.batching.driver');