mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 04:34:03 +00:00
CanDeleteKeys interface
This commit is contained in:
parent
bdab188a61
commit
c8e8c838db
6 changed files with 82 additions and 5 deletions
19
src/Contracts/Future/CanDeleteKeys.php
Normal file
19
src/Contracts/Future/CanDeleteKeys.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Contracts\Future;
|
||||
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
/**
|
||||
* This interface will be part of the StorageDriver interface in 3.x.
|
||||
*/
|
||||
interface CanDeleteKeys
|
||||
{
|
||||
/**
|
||||
* Delete keys from the storage.
|
||||
*
|
||||
* @param string[] $keys
|
||||
* @return void
|
||||
*/
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void;
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\StorageDrivers\Database;
|
|||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||
|
|
@ -16,7 +17,7 @@ use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains;
|
|||
use Stancl\Tenancy\StorageDrivers\Database\TenantModel as Tenants;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class DatabaseStorageDriver implements StorageDriver
|
||||
class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
|
@ -76,8 +77,7 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return Tenant
|
||||
* @throws TenantCouldNotBeIdentifiedException
|
||||
* @throws NotImplementedException
|
||||
* @throws TenantCouldNotBeIdentifiedException // todo ?
|
||||
*/
|
||||
public function findBy(string $key, $value): Tenant
|
||||
{
|
||||
|
|
@ -215,6 +215,12 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
$tenant = $tenant ?? $this->currentTenant();
|
||||
Tenants::find($tenant->id)->putMany($kvPairs);
|
||||
}
|
||||
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
Tenants::find($tenant->id)->deleteMany($keys);
|
||||
}
|
||||
}
|
||||
|
||||
class TenantModelTODO
|
||||
|
|
|
|||
|
|
@ -139,4 +139,23 @@ class TenantModel extends Model
|
|||
$this->dataColumn() => json_encode($jsonObj),
|
||||
]));
|
||||
}
|
||||
|
||||
public function deleteKeys(array $keys)
|
||||
{
|
||||
$customColumns = [];
|
||||
$jsonObj = json_decode($this->{$this->dataColumn()});
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (in_array($key, $this->customColumns())) {
|
||||
$customColumns[$key] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($jsonObj->$key);
|
||||
}
|
||||
|
||||
$this->update(array_merge($customColumns, [
|
||||
$this->dataColumn() => json_encode($jsonObj),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\StorageDrivers;
|
|||
|
||||
use Illuminate\Contracts\Redis\Factory as Redis;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
|
|
@ -13,7 +14,7 @@ use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
|||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class RedisStorageDriver implements StorageDriver
|
||||
class RedisStorageDriver implements StorageDriver, CanDeleteKeys
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
|
@ -232,4 +233,11 @@ class RedisStorageDriver implements StorageDriver
|
|||
|
||||
$this->redis->hmset("tenants:{$tenant->id}", $kvPairs);
|
||||
}
|
||||
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void
|
||||
{
|
||||
$tenant = $tenant ?? $this->tenant();
|
||||
|
||||
$this->redis->hdel("tenants:{$tenant->id}", $keys);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,10 @@ use Closure;
|
|||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Traits\ForwardsCalls;
|
||||
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
use Stancl\Tenancy\Exceptions\NotImplementedException;
|
||||
use Stancl\Tenancy\Exceptions\TenantStorageException;
|
||||
|
||||
/**
|
||||
|
|
@ -349,6 +351,29 @@ class Tenant implements ArrayAccess
|
|||
return $this->put($key, $value);
|
||||
}
|
||||
|
||||
// todo also deleteKey()?
|
||||
|
||||
/**
|
||||
* Delete keys from the tenant's storage.
|
||||
*
|
||||
* @param string|string[] $keys
|
||||
* @return self
|
||||
*/
|
||||
public function deleteKeys($keys): self
|
||||
{
|
||||
$keys = (array) $keys;
|
||||
|
||||
if (! $this->storage instanceof CanDeleteKeys) {
|
||||
throw new NotImplementedException(get_class($this->storage), 'deleteMany',
|
||||
'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.'
|
||||
);
|
||||
} else {
|
||||
$this->storage->deleteMany($keys);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -231,7 +231,7 @@ class TenantManager
|
|||
throw new Exception('No value supplied.');
|
||||
}
|
||||
|
||||
if (! method_exists($this->storage, 'findBy')) {
|
||||
if (! method_exists($this->storage, 'findBy')) { // todo use a contract instead
|
||||
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.'
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue