1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-18 08:44:03 +00:00

[1.7.0] Add DB storage driver (#82)

This commit is contained in:
Samuel Štancl 2019-08-16 14:36:49 +02:00 committed by GitHub
parent 674f4b3f9a
commit 9df78eb9c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 375 additions and 25 deletions

View file

@ -0,0 +1,84 @@
<?php
namespace Stancl\Tenancy\StorageDrivers;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Interfaces\StorageDriver;
class DatabaseStorageDriver implements StorageDriver
{
public $useJson = false;
// todo use an instance of tenant model?
// todo write tests verifying that data is decoded and added to the array
public function identifyTenant(string $domain): array
{
$id = $this->getTenantIdByDomain($domain);
if (! $id) {
throw new \Exception("Tenant could not be identified on domain {$domain}");
}
return $this->getTenantById($id);
}
/**
* Get information about the tenant based on his uuid.
*
* @param string $uuid
* @param array $fields
* @return array
*/
public function getTenantById(string $uuid, array $fields = []): array
{
if ($fields) {
return Tenant::decodeData(Tenant::find($uuid)->only($fields));
} else {
return Tenant::find($uuid)->decoded();
}
}
public function getTenantIdByDomain(string $domain): ?string
{
return Tenant::where('domain', $domain)->first()->uuid ?? null;
}
public function createTenant(string $domain, string $uuid): array
{
return Tenant::create(['uuid' => $uuid, 'domain' => $domain])->toArray();
}
public function deleteTenant(string $id): bool
{
return Tenant::find($id)->delete();
}
public function getAllTenants(array $uuids = []): array
{
return Tenant::getAllTenants($uuids)->toArray();
}
public function get(string $uuid, string $key)
{
return Tenant::find($uuid)->get($key);
}
public function getMany(string $uuid, array $keys): array
{
return Tenant::find($uuid)->getMany($keys);
}
public function put(string $uuid, string $key, $value)
{
return Tenant::find($uuid)->put($key, $value);
}
public function putMany(string $uuid, array $values): array
{
foreach ($values as $key => $value) {
Tenant::find($uuid)->put($key, $value);
}
return $values;
}
}

View file

@ -11,7 +11,7 @@ class RedisStorageDriver implements StorageDriver
public function __construct()
{
$this->redis = Redis::connection('tenancy');
$this->redis = Redis::connection(config('tenancy.redis.connection', 'tenancy'));
}
public function identifyTenant(string $domain): array
@ -33,8 +33,6 @@ class RedisStorageDriver implements StorageDriver
*/
public function getTenantById(string $uuid, array $fields = []): array
{
$fields = (array) $fields;
if (! $fields) {
return $this->redis->hgetall("tenants:$uuid");
}