refactor(security): update exception handling to use value objects
Refactor exception handling in ClientNameAlreadyExistsException and ClientNotFoundException to utilize value objects directly, improving type safety and clarity.
This commit is contained in:
parent
4d68a97889
commit
66fc714ca0
8 changed files with 14 additions and 10 deletions
|
|
@ -4,10 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\Security\Exceptions;
|
||||
|
||||
use Application\Security\Models\ValueObjects\ClientName;
|
||||
|
||||
final class ClientNameAlreadyExistsException extends \DomainException
|
||||
{
|
||||
public static function forName(string $name): self
|
||||
public static function forName(ClientName $name): self
|
||||
{
|
||||
return new self("A client with the name '{$name}' (or similar) already exists.");
|
||||
return new self("A client with the name '{$name->value}' (or similar) already exists.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\Security\Exceptions;
|
||||
|
||||
use Application\Security\Models\ValueObjects\ClientIdentifier;
|
||||
use Application\Security\Models\ValueObjects\Entities\ClientId;
|
||||
use Shared\DDD\Exception\NotFoundException;
|
||||
|
||||
final class ClientNotFoundException extends NotFoundException
|
||||
{
|
||||
public static function forClient(string $clientId): self
|
||||
public static function forClient(ClientId | ClientIdentifier $clientId): self
|
||||
{
|
||||
return new self("Client '{$clientId}' not found.");
|
||||
return new self("Client '{$clientId->value}' not found.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ final readonly class CreateClient implements Usecase
|
|||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
if ($this->clientRepository->existsByName($input->name)) {
|
||||
throw ClientNameAlreadyExistsException::forName($input->name->value);
|
||||
throw ClientNameAlreadyExistsException::forName($input->name);
|
||||
}
|
||||
|
||||
$clientId = $input->clientId ?? new ClientId(Uuid::uuid7());
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ final readonly class RegenerateClientSecret implements Usecase
|
|||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
$client = $this->clientRepository->find($input->clientId) ?? throw ClientNotFoundException::forClient($input->clientId->value);
|
||||
$client = $this->clientRepository->find($input->clientId) ?? throw ClientNotFoundException::forClient($input->clientId);
|
||||
|
||||
$newSecret = $this->credentialGenerator->generateSecret();
|
||||
$client->regenerateSecret($newSecret);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ final readonly class SetClientStatus implements Usecase
|
|||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
$client = $this->clientRepository->findByIdentifier($input->clientIdentifier)
|
||||
?? throw ClientNotFoundException::forClient($input->clientIdentifier->value);
|
||||
?? throw ClientNotFoundException::forClient($input->clientIdentifier);
|
||||
|
||||
$changed = $client->isActive !== $input->desiredStatus;
|
||||
$isActive = $client->setActive($input->desiredStatus);
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ final class ApiClientRepository implements ClientDirectory, ClientRepository
|
|||
{
|
||||
$deleted = ApiClient::query()->where('id', $client->id->value)->delete();
|
||||
if ($deleted === 0) {
|
||||
throw ClientNotFoundException::forClient($client->id->value);
|
||||
throw ClientNotFoundException::forClient($client->id);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ final class InMemoryClientRepository implements ClientDirectory, ClientRepositor
|
|||
public function delete(Client $client): void
|
||||
{
|
||||
if (! isset($this->clients[$client->id->value])) {
|
||||
throw ClientNotFoundException::forClient($client->id->value);
|
||||
throw ClientNotFoundException::forClient($client->id);
|
||||
}
|
||||
|
||||
unset($this->clients[$client->id->value]);
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ final class CreateClientCommand extends Command
|
|||
$clientName = new ClientName($trimmed);
|
||||
|
||||
if ($this->clientRepository->existsByName($clientName)) {
|
||||
return ClientNameAlreadyExistsException::forName($trimmed)->getMessage();
|
||||
return ClientNameAlreadyExistsException::forName($clientName)->getMessage();
|
||||
}
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return $e->getMessage();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue