generates token via injected TokenGenerator, asks Clock for now, sets expiry to now+7d, persists through SessionRepository->create and returns the resulting Session. all 31 tests pass.
34 lines
823 B
PHP
34 lines
823 B
PHP
<?php
|
|
|
|
namespace App\Auth\UseCases\CreateSession;
|
|
|
|
use App\Auth\Clock;
|
|
use App\Auth\CreateSessionDto;
|
|
use App\Auth\Session;
|
|
use App\Auth\SessionRepository;
|
|
use App\Auth\TokenGenerator;
|
|
use App\User\User;
|
|
|
|
class CreateSession
|
|
{
|
|
private const SESSION_LIFETIME = '+7 days';
|
|
|
|
public function __construct(
|
|
private SessionRepository $sessionRepo,
|
|
private TokenGenerator $tokenGenerator,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
public function execute(User $user): Session
|
|
{
|
|
$now = $this->clock->now();
|
|
$expiresAt = $now->modify(self::SESSION_LIFETIME);
|
|
|
|
return $this->sessionRepo->create(new CreateSessionDto(
|
|
token: $this->tokenGenerator->generate(),
|
|
user: $user,
|
|
createdAt: $now,
|
|
expiresAt: $expiresAt,
|
|
));
|
|
}
|
|
}
|