1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 09:54:05 +00:00

Add TenantDoesNotExist exception, fix queued migrator serialization

This commit is contained in:
Samuel Štancl 2019-10-16 08:37:30 +02:00
parent e98db460ec
commit 2f3924531d
5 changed files with 41 additions and 6 deletions

View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy\Exceptions;
use Exception;
class TenantDoesNotExistException extends Exception
{
public function __construct(string $id)
{
$this->message = "Tenant with this id does not exist: $id";
}
}

View file

@ -16,12 +16,12 @@ class QueuedTenantDatabaseMigrator implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var Tenant */ /** @var string */
protected $tenant; protected $tenantId;
public function __construct(Tenant $tenant) public function __construct(Tenant $tenant)
{ {
$this->tenant = $tenant; $this->tenantId = $tenant->id;
} }
/** /**
@ -32,7 +32,7 @@ class QueuedTenantDatabaseMigrator implements ShouldQueue
public function handle() public function handle()
{ {
Artisan::call('tenants:migrate', [ Artisan::call('tenants:migrate', [
'--tenants' => [$this->tenant->id], '--tenants' => [$this->tenantId],
]); ]);
} }
} }

View file

@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains; use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains;
use Stancl\Tenancy\StorageDrivers\Database\TenantModel as Tenants; use Stancl\Tenancy\StorageDrivers\Database\TenantModel as Tenants;
@ -59,7 +60,13 @@ class DatabaseStorageDriver implements StorageDriver
public function findById(string $id): Tenant public function findById(string $id): Tenant
{ {
return Tenant::fromStorage(Tenants::find($id)->decoded()) $tenant = Tenants::find($id);
if (! $tenant) {
throw new TenantDoesNotExistException($id);
}
return Tenant::fromStorage($tenant->decoded())
->withDomains($this->getTenantDomains($id)); ->withDomains($this->getTenantDomains($id));
} }

View file

@ -9,6 +9,7 @@ use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
@ -73,7 +74,13 @@ class RedisStorageDriver implements StorageDriver
public function findById(string $id): Tenant public function findById(string $id): Tenant
{ {
return $this->makeTenant($this->redis->hgetall("tenants:$id")); $data = $this->redis->hgetall("tenants:$id");
if (! $data) {
throw new TenantDoesNotExistException($id);
}
return $this->makeTenant($data);
} }
public function getTenantIdByDomain(string $domain): ?string public function getTenantIdByDomain(string $domain): ?string

View file

@ -8,6 +8,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
@ -266,4 +267,11 @@ class TenantManagerTest extends TestCase
(new QueuedTenantDatabaseMigrator($tenant))->handle(); (new QueuedTenantDatabaseMigrator($tenant))->handle();
$this->assertTrue(\Schema::hasTable('users')); $this->assertTrue(\Schema::hasTable('users'));
} }
/** @test */
public function TenantDoesNotExistException_is_thrown_when_find_is_called_on_an_id_that_does_not_belong_to_any_tenant()
{
$this->expectException(TenantDoesNotExistException::class);
tenancy()->find('gjnfdgf');
}
} }