2026-08-18 12:03:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Infra\Adapters\Repositories;
|
|
|
|
|
|
|
|
|
|
use Application\Security\Models\Entities\Client;
|
|
|
|
|
use Application\Security\Models\ValueObjects\ClientIdentifier;
|
2026-08-18 17:31:29 +02:00
|
|
|
use Application\Security\Models\ValueObjects\ClientName;
|
2026-08-18 12:03:29 +02:00
|
|
|
use Application\Security\Models\ValueObjects\Entities\ClientId;
|
|
|
|
|
use Application\Security\Ports\Repositories\ClientRepository;
|
2026-08-18 17:31:29 +02:00
|
|
|
use Illuminate\Support\Str;
|
2026-08-18 12:03:29 +02:00
|
|
|
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,
|
2026-08-18 17:31:29 +02:00
|
|
|
'slug' => Str::slug($client->name->value),
|
2026-08-18 12:03:29 +02:00
|
|
|
'identifier' => $client->identifier,
|
|
|
|
|
'secret' => $client->secret,
|
|
|
|
|
'is_active' => $client->isActive,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 17:31:29 +02:00
|
|
|
#[\Override]
|
|
|
|
|
public function existsByName(ClientName $name): bool
|
|
|
|
|
{
|
|
|
|
|
return ApiClient::query()
|
|
|
|
|
->where('slug', Str::slug($name->value))
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 12:03:29 +02:00
|
|
|
#[\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,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|