mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 06:34:04 +00:00
Refactor cached tenant resolvers with decorator pattern
This commit is contained in:
parent
8e3b74f9d1
commit
97e45ab9cc
15 changed files with 424 additions and 251 deletions
41
tests/Repository/InMemoryTenantRepository.php
Normal file
41
tests/Repository/InMemoryTenantRepository.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Repository;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Repository\TenantRepository;
|
||||
|
||||
class InMemoryTenantRepository implements TenantRepository
|
||||
{
|
||||
public function __construct(
|
||||
/** @var Collection<Tenant> */
|
||||
private Collection $tenants = new Collection(),
|
||||
) {
|
||||
}
|
||||
|
||||
public function find(int|string $id): ?Tenant
|
||||
{
|
||||
return $this->tenants->first(fn (Tenant $tenant) => $tenant->getTenantKey() == $id);
|
||||
}
|
||||
|
||||
public function findForDomain(string $domain): ?Tenant
|
||||
{
|
||||
return $this->tenants->firstWhere(fn (Tenant $tenant) => in_array($domain, $tenant->domains ?? []));
|
||||
}
|
||||
|
||||
public function all(): iterable
|
||||
{
|
||||
return $this->tenants->lazy();
|
||||
}
|
||||
|
||||
public function whereKeyIn(string|int ...$ids): iterable
|
||||
{
|
||||
return $this->tenants->filter(fn (Tenant $tenant) => in_array($tenant->getTenantKey(), $ids))->lazy();
|
||||
}
|
||||
|
||||
public function store(Tenant $tenant): void
|
||||
{
|
||||
$this->tenants->push($tenant);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue