1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 02:54:03 +00:00

CanDeleteKeys interface

This commit is contained in:
Samuel Štancl 2019-10-27 11:35:24 +01:00
parent bdab188a61
commit c8e8c838db
6 changed files with 82 additions and 5 deletions

View 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;
}

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy\StorageDrivers\Database;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; 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\StorageDrivers\Database\TenantModel as Tenants;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
class DatabaseStorageDriver implements StorageDriver class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
{ {
/** @var Application */ /** @var Application */
protected $app; protected $app;
@ -76,8 +77,7 @@ class DatabaseStorageDriver implements StorageDriver
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @return Tenant * @return Tenant
* @throws TenantCouldNotBeIdentifiedException * @throws TenantCouldNotBeIdentifiedException // todo ?
* @throws NotImplementedException
*/ */
public function findBy(string $key, $value): Tenant public function findBy(string $key, $value): Tenant
{ {
@ -215,6 +215,12 @@ class DatabaseStorageDriver implements StorageDriver
$tenant = $tenant ?? $this->currentTenant(); $tenant = $tenant ?? $this->currentTenant();
Tenants::find($tenant->id)->putMany($kvPairs); 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 class TenantModelTODO

View file

@ -139,4 +139,23 @@ class TenantModel extends Model
$this->dataColumn() => json_encode($jsonObj), $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),
]));
}
} }

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy\StorageDrivers;
use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
@ -13,7 +14,7 @@ use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
class RedisStorageDriver implements StorageDriver class RedisStorageDriver implements StorageDriver, CanDeleteKeys
{ {
/** @var Application */ /** @var Application */
protected $app; protected $app;
@ -232,4 +233,11 @@ class RedisStorageDriver implements StorageDriver
$this->redis->hmset("tenants:{$tenant->id}", $kvPairs); $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);
}
} }

View file

@ -9,8 +9,10 @@ use Closure;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\ForwardsCalls;
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Exceptions\NotImplementedException;
use Stancl\Tenancy\Exceptions\TenantStorageException; use Stancl\Tenancy\Exceptions\TenantStorageException;
/** /**
@ -349,6 +351,29 @@ class Tenant implements ArrayAccess
return $this->put($key, $value); 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. * Set a value.
* *

View file

@ -231,7 +231,7 @@ class TenantManager
throw new Exception('No value supplied.'); 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', 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 storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.'
); );