add create session use case
This commit is contained in:
parent
2a281386a5
commit
05f4f334e6
1 changed files with 34 additions and 0 deletions
34
app/Auth/UseCases/CreateSession.php
Normal file
34
app/Auth/UseCases/CreateSession.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases;
|
||||
|
||||
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(),
|
||||
userId: $user->getId(),
|
||||
createdAt: $now,
|
||||
expiresAt: $expiresAt,
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue