1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 20:34:03 +00:00
tenancy/src/Bootstrappers/JobBatchBootstrapper.php
Samuel Štancl 9f94505cb4
Rename bootstrappers (#40)
* SessionTenancyBootstrapper -> DatabaseSessionBootstrapper

* FortifyRouteTenancyBootstrapper -> FortifyRouteBootstrapper

* BatchTenancyBootstrapper -> JobBatchBootstrapper

* ScoutTenancyBootstrapper -> ScoutPrefixBootstrapper, also fix logic and remove todo

* MailTenancyBootstrapper -> MailConfigBootstrapper

* PrefixCacheTenancyBootstrapper -> CacheTenancyBootstrapper

* remove todo

* improve config file
2024-03-28 03:18:23 +01:00

44 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
/**
* Adds support for running queued tenant jobs in batches.
*/
class JobBatchBootstrapper implements TenancyBootstrapper
{
/**
* The previous database connection instance.
*/
protected ?Connection $previousConnection = null;
public function __construct(
protected DatabaseBatchRepository $batchRepository,
protected DatabaseManager $databaseManager
) {
}
public function bootstrap(Tenant $tenant): void
{
// Update batch repository connection to use the tenant connection
$this->previousConnection = $this->batchRepository->getConnection();
$this->batchRepository->setConnection($this->databaseManager->connection('tenant'));
}
public function revert(): void
{
if ($this->previousConnection) {
// Replace batch repository connection with the previously replaced one
$this->batchRepository->setConnection($this->previousConnection);
$this->previousConnection = null;
}
}
}