1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

CachedTenantResolver

This commit is contained in:
Samuel Štancl 2020-05-20 20:34:16 +02:00
parent e6e4548a22
commit d7536ce0af
3 changed files with 77 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<?php
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Cache\Repository;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
class CachedTenantResolver implements TenantResolver
{
/** @var Repository */
protected $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
public function resolve(...$args): Tenant
{
$resolverClass = $args[0];
$data = $args[1];
/** @var TenantResolver $resolver */
$resolver = app($resolverClass);
$encodedData = json_encode($data);
if ($this->cache->has($key = "_tenancy_resolver:$resolverClass:$encodedData")) {
return $this->cache->get($key);
}
$this->cache->put($key,
$resolved = $resolver->resolve(...$data)
);
return $resolved;
}
}