mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:34:05 +00:00
findBy DB storage driver
This commit is contained in:
parent
a80275c1ac
commit
8d73381eae
2 changed files with 44 additions and 2 deletions
|
|
@ -8,8 +8,8 @@ use Exception;
|
||||||
|
|
||||||
class TenantDoesNotExistException extends 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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,28 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
->withDomains($this->getTenantDomains($id));
|
->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)
|
protected function getTenantDomains($id)
|
||||||
{
|
{
|
||||||
return Domains::where('tenant_id', $id)->get()->map(function ($model) {
|
return Domains::where('tenant_id', $id)->get()->map(function ($model) {
|
||||||
|
|
@ -194,3 +216,23 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
Tenants::find($tenant->id)->putMany($kvPairs);
|
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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue