90 lines
2.6 KiB
PHP
90 lines
2.6 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 Infra\Database\Models\ApiClient;
|
||
|
|
use Shared\Paginated;
|
||
|
|
use Shared\Pagination;
|
||
|
|
|
||
|
|
final class ApiClientRepository implements ClientRepository
|
||
|
|
{
|
||
|
|
#[\Override]
|
||
|
|
public function persist(Client $client): void
|
||
|
|
{
|
||
|
|
ApiClient::query()->updateOrCreate(
|
||
|
|
['id' => $client->id->value],
|
||
|
|
[
|
||
|
|
'name' => $client->name,
|
||
|
|
'identifier' => $client->identifier,
|
||
|
|
'secret' => $client->secret,
|
||
|
|
'is_active' => $client->isActive,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function findByIdentifier(ClientIdentifier $identifier): ?Client
|
||
|
|
{
|
||
|
|
return transform(
|
||
|
|
ApiClient::query()
|
||
|
|
->where('identifier', $identifier->value)
|
||
|
|
->first(),
|
||
|
|
fn (ApiClient $apiClient) => $this->toDomain($apiClient)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function find(ClientId $id): ?Client
|
||
|
|
{
|
||
|
|
return transform(
|
||
|
|
ApiClient::query()->where('id', $id->value)->first(),
|
||
|
|
fn (ApiClient $apiClient) => $this->toDomain($apiClient)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function findAll(Pagination $pagination): Paginated
|
||
|
|
{
|
||
|
|
$query = ApiClient::query();
|
||
|
|
$total = $query->count();
|
||
|
|
$items = $query->forPage(
|
||
|
|
$pagination->pageNumber(),
|
||
|
|
$pagination->pageSize()
|
||
|
|
)->get();
|
||
|
|
|
||
|
|
return new Paginated(
|
||
|
|
pagination: $pagination,
|
||
|
|
items: $items->map(fn (ApiClient $apiClient) => $this->toDomain($apiClient))->all(),
|
||
|
|
total: $total,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function delete(Client $client): void
|
||
|
|
{
|
||
|
|
$deleted = ApiClient::query()->where('id', $client->id->value)->delete();
|
||
|
|
if ($deleted === 0) {
|
||
|
|
throw new \LogicException('Client not found');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function toDomain(ApiClient $apiClient): Client
|
||
|
|
{
|
||
|
|
return Client::hydrate(
|
||
|
|
id: new ClientId($apiClient->id),
|
||
|
|
name: $apiClient->name,
|
||
|
|
identifier: $apiClient->identifier,
|
||
|
|
secret: $apiClient->secret,
|
||
|
|
isActive: $apiClient->is_active,
|
||
|
|
createdAt: $apiClient->created_at,
|
||
|
|
updatedAt: $apiClient->updated_at,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|