mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 05:14:03 +00:00
CanFindByAnyKey interface
This commit is contained in:
parent
081799e233
commit
d61117c3b6
5 changed files with 28 additions and 7 deletions
22
src/Contracts/Future/CanFindByAnyKey.php
Normal file
22
src/Contracts/Future/CanFindByAnyKey.php
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Contracts\Future;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface *might* be part of the StorageDriver interface in 3.x.
|
||||||
|
*/
|
||||||
|
interface CanFindByAnyKey
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Find a tenant using an arbitrary key.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @return Tenant
|
||||||
|
* @throws TenantDoesNotExistException
|
||||||
|
*/
|
||||||
|
public function findBy(string $key, $value): Tenant;
|
||||||
|
}
|
||||||
|
|
@ -77,11 +77,11 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return Tenant
|
* @return Tenant
|
||||||
* @throws TenantCouldNotBeIdentifiedException // todo ?
|
* @throws TenantDoesNotExistException
|
||||||
*/
|
*/
|
||||||
public function findBy(string $key, $value): Tenant
|
public function findBy(string $key, $value): Tenant
|
||||||
{
|
{
|
||||||
// [WIP] [TODO] Temporary implementation, key has to be a custom column.
|
// The key has to be a custom column. It's recommended to set up an index
|
||||||
$tenant = Tenants::where($key, $value)->first();
|
$tenant = Tenants::where($key, $value)->first();
|
||||||
|
|
||||||
if (! $tenant) {
|
if (! $tenant) {
|
||||||
|
|
|
||||||
|
|
@ -84,8 +84,6 @@ class RedisStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
return $this->makeTenant($data);
|
return $this->makeTenant($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO find by *
|
|
||||||
|
|
||||||
public function getTenantIdByDomain(string $domain): ?string
|
public function getTenantIdByDomain(string $domain): ?string
|
||||||
{
|
{
|
||||||
return $this->redis->hget("domains:$domain", 'tenant_id') ?: null;
|
return $this->redis->hget("domains:$domain", 'tenant_id') ?: null;
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Support\Traits\ForwardsCalls;
|
use Illuminate\Support\Traits\ForwardsCalls;
|
||||||
|
use Stancl\Tenancy\Contracts\Future\CanFindByAnyKeys;
|
||||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
use Stancl\Tenancy\Exceptions\NotImplementedException;
|
use Stancl\Tenancy\Exceptions\NotImplementedException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
|
@ -231,9 +232,9 @@ class TenantManager
|
||||||
throw new Exception('No value supplied.');
|
throw new Exception('No value supplied.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! method_exists($this->storage, 'findBy')) { // todo use a contract instead
|
if (! $this->storage instanceof CanFindByAnyKeys) {
|
||||||
throw new NotImplementedException(get_class($this->storage), 'findBy',
|
throw new NotImplementedException(get_class($this->storage), 'findBy',
|
||||||
'This method was added to storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.'
|
'This method was added to the DB storage driver provided by the package in 2.2.0 and might be part of the StorageDriver contract in 3.0.0.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue