mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
Get Redis tests to pass
This commit is contained in:
parent
6b0ec1b554
commit
fb58f21f1c
3 changed files with 51 additions and 30 deletions
|
|
@ -59,26 +59,12 @@ class RedisStorageDriver implements StorageDriver
|
|||
throw new TenantCouldNotBeIdentifiedException($domain);
|
||||
}
|
||||
|
||||
return $this->find($id);
|
||||
return $this->findById($id);
|
||||
}
|
||||
|
||||
public function findById(string $id): Tenant
|
||||
{
|
||||
$data = $this->redis->hgetall("tenants:$id");
|
||||
$keys = [];
|
||||
$values = [];
|
||||
foreach ($data as $i => $value) {
|
||||
if ($i & 1) { // is odd
|
||||
$values[] = $value;
|
||||
} else {
|
||||
$keys[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$data = array_combine($keys, $values);
|
||||
$domains = []; // todo2
|
||||
|
||||
return Tenant::fromStorage($data)->withDomains($domains);
|
||||
return $this->makeTenant($this->redis->hgetall("tenants:$id"));
|
||||
}
|
||||
|
||||
public function getTenantIdByDomain(string $domain): ?string
|
||||
|
|
@ -89,19 +75,23 @@ class RedisStorageDriver implements StorageDriver
|
|||
public function createTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
||||
$id = $tenant->id;
|
||||
|
||||
foreach ($tenant->domains as $domain) {
|
||||
$pipe->hmset("domains:$domain", 'tenant_id', $id);
|
||||
$pipe->hmset("domains:$domain", ['tenant_id' => $tenant->id]);
|
||||
}
|
||||
$pipe->hmset("tenants:$id", 'id', json_encode($id), 'domain', json_encode($domain));
|
||||
|
||||
$data = [];
|
||||
foreach ($tenant->data as $key => $value) {
|
||||
$data[$key] = json_encode($value);
|
||||
}
|
||||
|
||||
$pipe->hmset("tenants:{$tenant->id}", array_merge($data, ['_tenancy_domains' => json_encode($tenant->domains)]));
|
||||
});
|
||||
}
|
||||
|
||||
public function updateTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
||||
$pipe->hmset("tenants:{$tenant->id}", $tenant->data);
|
||||
$pipe->hmset("tenants:{$tenant->id}", $tenant->data); // todo domains
|
||||
|
||||
foreach ($tenant->domains as $domain) {
|
||||
$pipe->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
||||
|
|
@ -122,6 +112,12 @@ class RedisStorageDriver implements StorageDriver
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all tenants.
|
||||
*
|
||||
* @param string[] $ids
|
||||
* @return Tenant[]
|
||||
*/
|
||||
public function all(array $ids = []): array
|
||||
{
|
||||
// todo2 $this->redis->pipeline()
|
||||
|
|
@ -143,10 +139,28 @@ class RedisStorageDriver implements StorageDriver
|
|||
}
|
||||
|
||||
return array_map(function ($tenant) {
|
||||
return $this->redis->hgetall($tenant);
|
||||
return $this->makeTenant($this->redis->hgetall($tenant));
|
||||
}, $hashes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a Tenant instance from low-level array data.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Tenant
|
||||
*/
|
||||
protected function makeTenant(array $data): Tenant
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
$data[$key] = json_decode($value, true);
|
||||
}
|
||||
|
||||
$domains = $data['_tenancy_domains'];
|
||||
unset($data['_tenancy_domains']);
|
||||
|
||||
return Tenant::fromStorage($data)->withDomains($domains);
|
||||
}
|
||||
|
||||
public function get(string $key, Tenant $tenant = null)
|
||||
{
|
||||
$tenant = $tenant ?? $this->tenant();
|
||||
|
|
@ -161,7 +175,7 @@ class RedisStorageDriver implements StorageDriver
|
|||
$result = [];
|
||||
$values = $this->redis->hmget("tenants:{$tenant->id}", $keys);
|
||||
foreach ($keys as $i => $key) {
|
||||
$result[$key] = $values[$i];
|
||||
$result[$key] = json_decode($values[$i], true);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue