From 2f3924531db458a85b3b26d55c439e89380be8c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 16 Oct 2019 08:37:30 +0200 Subject: [PATCH] Add TenantDoesNotExist exception, fix queued migrator serialization --- src/Exceptions/TenantDoesNotExistException.php | 13 +++++++++++++ src/Jobs/QueuedTenantDatabaseMigrator.php | 8 ++++---- .../Database/DatabaseStorageDriver.php | 9 ++++++++- src/StorageDrivers/RedisStorageDriver.php | 9 ++++++++- tests/TenantManagerTest.php | 8 ++++++++ 5 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/TenantDoesNotExistException.php diff --git a/src/Exceptions/TenantDoesNotExistException.php b/src/Exceptions/TenantDoesNotExistException.php new file mode 100644 index 00000000..d181a14c --- /dev/null +++ b/src/Exceptions/TenantDoesNotExistException.php @@ -0,0 +1,13 @@ +message = "Tenant with this id does not exist: $id"; + } +} \ No newline at end of file diff --git a/src/Jobs/QueuedTenantDatabaseMigrator.php b/src/Jobs/QueuedTenantDatabaseMigrator.php index 84acca66..5ea12656 100644 --- a/src/Jobs/QueuedTenantDatabaseMigrator.php +++ b/src/Jobs/QueuedTenantDatabaseMigrator.php @@ -16,12 +16,12 @@ class QueuedTenantDatabaseMigrator implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - /** @var Tenant */ - protected $tenant; + /** @var string */ + protected $tenantId; public function __construct(Tenant $tenant) { - $this->tenant = $tenant; + $this->tenantId = $tenant->id; } /** @@ -32,7 +32,7 @@ class QueuedTenantDatabaseMigrator implements ShouldQueue public function handle() { Artisan::call('tenants:migrate', [ - '--tenants' => [$this->tenant->id], + '--tenants' => [$this->tenantId], ]); } } diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index 4f5c64bb..95df006e 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; +use Stancl\Tenancy\Exceptions\TenantDoesNotExistException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains; use Stancl\Tenancy\StorageDrivers\Database\TenantModel as Tenants; @@ -59,7 +60,13 @@ class DatabaseStorageDriver implements StorageDriver 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)); } diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 4e35834f..862f5069 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -9,6 +9,7 @@ use Illuminate\Foundation\Application; use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; +use Stancl\Tenancy\Exceptions\TenantDoesNotExistException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Tenant; @@ -73,7 +74,13 @@ class RedisStorageDriver implements StorageDriver 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 diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 7f8aae0d..f109236b 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; +use Stancl\Tenancy\Exceptions\TenantDoesNotExistException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator; use Stancl\Tenancy\Tenant; @@ -266,4 +267,11 @@ class TenantManagerTest extends TestCase (new QueuedTenantDatabaseMigrator($tenant))->handle(); $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'); + } }