format backend code
This commit is contained in:
parent
9577367622
commit
67dd376a2f
40 changed files with 146 additions and 71 deletions
|
|
@ -12,5 +12,6 @@ class CreateSessionDto
|
|||
public User $user,
|
||||
public DateTimeImmutable $createdAt,
|
||||
public DateTimeImmutable $expiresAt,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ use DateTimeZone;
|
|||
|
||||
class EloquentSessionRepository implements SessionRepository
|
||||
{
|
||||
public function __construct(private UserRepository $userRepo) {}
|
||||
public function __construct(private UserRepository $userRepo)
|
||||
{
|
||||
}
|
||||
|
||||
public function create(CreateSessionDto $dto): Session
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ class Session
|
|||
private User $user,
|
||||
private DateTimeImmutable $createdAt,
|
||||
private DateTimeImmutable $expiresAt,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ class AuthenticateUser
|
|||
public function __construct(
|
||||
private UserRepository $userRepo,
|
||||
private PasswordHasher $hasher,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
|
|
|
|||
|
|
@ -7,5 +7,6 @@ class AuthenticateUserRequest
|
|||
public function __construct(
|
||||
public ?string $email,
|
||||
public ?string $password,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ class CreateSession
|
|||
private SessionRepository $sessionRepo,
|
||||
private TokenGenerator $tokenGenerator,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(User $user): Session
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ class Logout
|
|||
{
|
||||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(string $token): void
|
||||
{
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ class AuthController
|
|||
private AuthenticateUser $authenticateUser,
|
||||
private CreateSession $createSession,
|
||||
private Logout $logout,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function login(Request $request): JsonResponse
|
||||
{
|
||||
|
|
@ -33,11 +34,13 @@ class AuthController
|
|||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse(
|
||||
['error' => $exception->getMessage()], 400
|
||||
['error' => $exception->getMessage()],
|
||||
400
|
||||
);
|
||||
} catch (UnauthorizedException $exception) {
|
||||
return new JsonResponse(
|
||||
['error' => $exception->getMessage()], 401
|
||||
['error' => $exception->getMessage()],
|
||||
401
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +74,7 @@ class AuthController
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array{id: int, email: string, firstname: string, lastname: string}
|
||||
* @return array{id: int, email: string}
|
||||
*/
|
||||
private function buildUserPayload(User $user): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ use Illuminate\Http\JsonResponse;
|
|||
|
||||
class SetController
|
||||
{
|
||||
public function __construct(private SetRepository $setRepository) {}
|
||||
public function __construct(private SetRepository $setRepository)
|
||||
{
|
||||
}
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,5 +10,6 @@ class CreateElementDto
|
|||
public Set $set,
|
||||
public string $title,
|
||||
public ?Element $parentElement,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ class Element
|
|||
private string $title,
|
||||
private Set $set,
|
||||
private ?Element $parentElement,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ use DomainException;
|
|||
|
||||
class EloquentElementRepository implements ElementRepository
|
||||
{
|
||||
public function __construct(private SetRepository $setRepo) {}
|
||||
public function __construct(private SetRepository $setRepo)
|
||||
{
|
||||
}
|
||||
|
||||
public function create(CreateElementDto $dto): Element
|
||||
{
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ class CreateElement
|
|||
public function __construct(
|
||||
private ElementRepository $elementRepo,
|
||||
private SetRepository $setRepo,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
|
|
|
|||
|
|
@ -8,5 +8,6 @@ class CreateElementRequest
|
|||
public ?int $setId,
|
||||
public ?string $title,
|
||||
public ?int $parentElementId,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,4 +4,6 @@ namespace App\Exceptions;
|
|||
|
||||
use DomainException;
|
||||
|
||||
class BadRequestException extends DomainException {}
|
||||
class BadRequestException extends DomainException
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,4 +4,6 @@ namespace App\Exceptions;
|
|||
|
||||
use DomainException;
|
||||
|
||||
class UnauthorizedException extends DomainException {}
|
||||
class UnauthorizedException extends DomainException
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ class AuthMiddleware
|
|||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(Request): Response $next
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ class CreateSetDto
|
|||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ class Set
|
|||
public function __construct(
|
||||
private int $id,
|
||||
private string $name,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,15 +18,15 @@ final readonly class EmailAddress
|
|||
$trimmed = trim($email);
|
||||
|
||||
if ($trimmed === '' || ! str_contains($trimmed, '@')) {
|
||||
throw new InvalidArgumentException(self::ERROR_MESSAGE." $email");
|
||||
throw new InvalidArgumentException(self::ERROR_MESSAGE . " $email");
|
||||
}
|
||||
|
||||
[$local, $domain] = explode('@', $trimmed, 2);
|
||||
$this->domain = mb_strtolower($domain);
|
||||
$normalized = $local.'@'.$this->domain;
|
||||
$normalized = $local . '@' . $this->domain;
|
||||
|
||||
if (filter_var($normalized, FILTER_VALIDATE_EMAIL) === false) {
|
||||
throw new InvalidArgumentException(self::ERROR_MESSAGE." $email");
|
||||
throw new InvalidArgumentException(self::ERROR_MESSAGE . " $email");
|
||||
}
|
||||
|
||||
$this->normalized = $normalized;
|
||||
|
|
|
|||
|
|
@ -9,5 +9,6 @@ class CreateUserDto
|
|||
public function __construct(
|
||||
public EmailAddress $email,
|
||||
public string $passwordHash,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ class EloquentUserRepository implements UserRepository
|
|||
|
||||
public function findByEmailDomain(string $domain): array
|
||||
{
|
||||
$models = UserModel::where('email', 'like', '%@'.$domain)->get();
|
||||
$models = UserModel::where('email', 'like', '%@' . $domain)->get();
|
||||
$users = [];
|
||||
foreach ($models as $model) {
|
||||
$users[] = $this->toDomain($model);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ class User
|
|||
private int $id,
|
||||
private EmailAddress $email,
|
||||
private string $passwordHash,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,12 +9,6 @@ use Illuminate\Database\Eloquent\Model;
|
|||
* @property string $email
|
||||
* @property string $password_hash
|
||||
*
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel whereEmail($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel whereId($value)
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class UserModel extends Model
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue