diff --git a/src/MongoDBBusServiceProvider.php b/src/MongoDBBusServiceProvider.php index fa8ca9a6c..005dd0aed 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,6 +21,12 @@ 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')), @@ -25,7 +34,8 @@ public function register() ); }); - /** @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'); @@ -39,6 +49,7 @@ public function register() public function provides() { return [ + BatchRepository::class, MongoBatchRepository::class, ]; }