1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 17:54:03 +00:00
tenancy/src/Bootstrappers/BatchTenancyBootstrapper.php
2022-09-23 10:57:54 +05:00

41 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class BatchTenancyBootstrapper implements TenancyBootstrapper
{
/**
* The previous database connection instance.
*
* @var \Illuminate\Database\Connection
*/
protected $previousConnection = null;
public function __construct(protected DatabaseBatchRepository $batchRepository)
{
}
public function bootstrap(Tenant $tenant)
{
// Update batch repository connection to use the tenant connection
$this->previousConnection = $this->batchRepository->getConnection();
$this->batchRepository->setConnection(DB::connection('tenant'));
}
public function revert()
{
if ($this->previousConnection) {
// Replace batch repository connection with the previously replaced one
$this->batchRepository->setConnection($this->previousConnection);
$this->previousConnection = null;
}
}
}