From 8d73381eae89a369bf2ac8a02fcd2d69e2b1cce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 24 Oct 2019 19:22:00 +0200 Subject: [PATCH] findBy DB storage driver --- .../TenantDoesNotExistException.php | 4 +- .../Database/DatabaseStorageDriver.php | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/TenantDoesNotExistException.php b/src/Exceptions/TenantDoesNotExistException.php index eb87701f..67770a44 100644 --- a/src/Exceptions/TenantDoesNotExistException.php +++ b/src/Exceptions/TenantDoesNotExistException.php @@ -8,8 +8,8 @@ use Exception; class TenantDoesNotExistException extends Exception { - public function __construct(string $id) + public function __construct(string $id, string $key = 'id') { - $this->message = "Tenant with this id does not exist: $id"; + $this->message = "Tenant with this $key does not exist: $id"; } } diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index 0b649eae..b91bafca 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -70,6 +70,28 @@ class DatabaseStorageDriver implements StorageDriver ->withDomains($this->getTenantDomains($id)); } + /** + * Find a tenant using an arbitrary key. + * + * @param string $key + * @param mixed $value + * @return Tenant + * @throws TenantCouldNotBeIdentifiedException + * @throws NotImplementedException + */ + public function findBy(string $key, $value): Tenant + { + // [WIP] [TODO] Temporary implementation, key has to be a custom column. + $tenant = Tenant::where($key, $value)->first(); + + if (! $tenant) { + throw new TenantDoesNotExistException($value, $key); + } + + return Tenant::fromStorage($tenant->decoded()) + ->withDomains($this->getTenantDomains($tenant->id)); + } + protected function getTenantDomains($id) { return Domains::where('tenant_id', $id)->get()->map(function ($model) { @@ -194,3 +216,23 @@ class DatabaseStorageDriver implements StorageDriver Tenants::find($tenant->id)->putMany($kvPairs); } } + +class TenantModelTODO +{ + public static function makeTenant($data, $domains) + { + // TODO + } + + public static function findBy(string $key, $value) + { + // TODO + } + + // TODO Use this w/ DB builder calls instead of an Eloquent model +} + +class DomainModelTODO +{ + // TODO Use this w/ DB builder calls instead of an Eloquent model +} \ No newline at end of file