diff --git a/src/Contracts/Future/CanDeleteKeys.php b/src/Contracts/Future/CanDeleteKeys.php new file mode 100644 index 00000000..990ed34e --- /dev/null +++ b/src/Contracts/Future/CanDeleteKeys.php @@ -0,0 +1,19 @@ +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 diff --git a/src/StorageDrivers/Database/TenantModel.php b/src/StorageDrivers/Database/TenantModel.php index 16189723..78d7ab16 100644 --- a/src/StorageDrivers/Database/TenantModel.php +++ b/src/StorageDrivers/Database/TenantModel.php @@ -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), + ])); + } } diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 167984c6..8d5fc14a 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -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); + } } diff --git a/src/Tenant.php b/src/Tenant.php index eb44466f..0c18fc0b 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -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. * diff --git a/src/TenantManager.php b/src/TenantManager.php index 83764160..21c2346b 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -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.' );