1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-14 06:54:04 +00:00

get down to 59 phpstan errors

This commit is contained in:
Samuel Štancl 2022-09-29 22:20:55 +02:00
parent 193e044777
commit a94227a19c
26 changed files with 130 additions and 130 deletions

View file

@ -15,25 +15,13 @@ use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
*/
class DatabaseManager
{
/** @var Application */
protected $app;
public function __construct(
protected Application $app,
protected BaseDatabaseManager $database,
protected Repository $config,
) {}
/** @var BaseDatabaseManager */
protected $database;
/** @var Repository */
protected $config;
public function __construct(Application $app, BaseDatabaseManager $database, Repository $config)
{
$this->app = $app;
$this->database = $database;
$this->config = $config;
}
/**
* Connect to a tenant's database.
*/
/** Connect to a tenant's database. */
public function connectToTenant(TenantWithDatabase $tenant): void
{
$this->purgeTenantConnection();
@ -41,35 +29,27 @@ class DatabaseManager
$this->setDefaultConnection('tenant');
}
/**
* Reconnect to the default non-tenant connection.
*/
/** Reconnect to the default non-tenant connection. */
public function reconnectToCentral(): void
{
$this->purgeTenantConnection();
$this->setDefaultConnection($this->config->get('tenancy.database.central_connection'));
}
/**
* Change the default database connection config.
*/
/** Change the default database connection config. */
public function setDefaultConnection(string $connection): void
{
$this->config['database.default'] = $connection;
$this->database->setDefaultConnection($connection);
}
/**
* Create the tenant database connection.
*/
/** Create the tenant database connection. */
public function createTenantConnection(TenantWithDatabase $tenant): void
{
$this->config['database.connections.tenant'] = $tenant->database()->connection();
}
/**
* Purge the tenant database connection.
*/
/** Purge the tenant database connection. */
public function purgeTenantConnection(): void
{
if (array_key_exists('tenant', $this->database->getConnections())) {
@ -83,8 +63,8 @@ class DatabaseManager
* Check if a tenant can be created.
*
* @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException
* @throws Exceptions\DatabaseManagerNotRegisteredException
* @throws Exceptions\TenantDatabaseAlreadyExistsException
*/
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
{
@ -94,8 +74,13 @@ class DatabaseManager
throw new Exceptions\TenantDatabaseAlreadyExistsException($database);
}
if ($manager instanceof Contracts\ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
throw new Exceptions\TenantDatabaseUserAlreadyExistsException($username);
if ($manager instanceof Contracts\ManagesDatabaseUsers) {
/** @var string $username */
$username = $tenant->database()->getUsername();
if ($manager->userExists($username)) {
throw new Exceptions\TenantDatabaseUserAlreadyExistsException($username);
}
}
}
}