75 lines
1.8 KiB
PHP
75 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Infra\Adapters\Repositories;
|
||
|
|
|
||
|
|
use Application\Security\Models\Entities\Client;
|
||
|
|
use Application\Security\Models\ValueObjects\ClientIdentifier;
|
||
|
|
use Application\Security\Models\ValueObjects\Entities\ClientId;
|
||
|
|
use Application\Security\Ports\Repositories\ClientRepository;
|
||
|
|
use Shared\Paginated;
|
||
|
|
use Shared\Pagination;
|
||
|
|
|
||
|
|
final class InMemoryClientRepository implements ClientRepository
|
||
|
|
{
|
||
|
|
/** @var array<string, Client> */
|
||
|
|
private array $clients = [];
|
||
|
|
|
||
|
|
public function add(Client ...$clients): self
|
||
|
|
{
|
||
|
|
foreach ($clients as $client) {
|
||
|
|
$this->clients[$client->id->value] = $client;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function clear(): void
|
||
|
|
{
|
||
|
|
$this->clients = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function find(ClientId $id): ?Client
|
||
|
|
{
|
||
|
|
return $this->clients[$id->value] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function persist(Client $client): void
|
||
|
|
{
|
||
|
|
$this->clients[$client->id->value] = $client;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function delete(Client $client): void
|
||
|
|
{
|
||
|
|
unset($this->clients[$client->id->value]);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function findByIdentifier(ClientIdentifier $identifier): ?Client
|
||
|
|
{
|
||
|
|
foreach ($this->clients as $client) {
|
||
|
|
if ($client->identifier->equals($identifier)) {
|
||
|
|
return $client;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function findAll(Pagination $pagination): Paginated
|
||
|
|
{
|
||
|
|
$items = array_values($this->clients);
|
||
|
|
$total = count($items);
|
||
|
|
$offset = $pagination->from();
|
||
|
|
$limit = $pagination->pageSize();
|
||
|
|
$paged = array_slice($items, $offset, $limit);
|
||
|
|
|
||
|
|
return new Paginated($pagination, $paged, $total);
|
||
|
|
}
|
||
|
|
}
|