Commit 33b912e0 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add token create use case

parent 93a25fed
......@@ -9,7 +9,6 @@ trait CastsTo
/**
* @param $date
* @param string $format
*
* @return \Carbon\Carbon|null
*/
protected function castToDate($date, $format = 'Y-m-d H:i:s'): ?Carbon
......@@ -20,4 +19,17 @@ trait CastsTo
return Carbon::createFromFormat($format, $date);
}
/**
* @param string|null $int
* @return int|null
*/
protected function castToInt(?string $int): ?int
{
if (!$int) {
return null;
}
return (int)$int;
}
}
......@@ -7,6 +7,7 @@ use Carbon\Carbon;
use Source\Entities\Door;
use Source\Entities\User;
use Source\Entities\Group;
use Source\Entities\Token;
abstract class BasePresenter
{
......@@ -97,4 +98,24 @@ abstract class BasePresenter
'updated_at' => $this->formatDateTime($door->getUpdatedAt()),
];
}
/**
* @param \Source\Entities\Token|null $token
* @return array
*/
public function formatToken(?Token $token): array
{
if (!$token) {
return [];
}
return [
'id' => $token->getId(),
'name' => $token->getName(),
'user_id' => $token->getUserId(),
'expires_at' => $this->formatDateTime($token->getExpiresAt()),
'created_at' => $this->formatDateTime($token->getCreatedAt()),
'updated_at' => $this->formatDateTime($token->getUpdatedAt()),
];
}
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['token_string'] = $responseModel->getTokenString();
$this->viewModel['token'] = $this->formatToken($responseModel->getToken());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
use Source\Entities\Token;
use Source\Sanitize\CastsTo;
use Source\Entities\HashedSearchable;
use Source\Gateways\Tokens\TokensRepository;
class CreateToken implements CreateTokenUseCase
{
use CastsTo;
/**
* @var \Source\Gateways\Tokens\TokensRepository
*/
protected TokensRepository $tokens;
/**
* @param \Source\Gateways\Tokens\TokensRepository $tokens
*/
public function __construct(TokensRepository $tokens)
{
$this->tokens = $tokens;
}
/**
* @inheritDoc
*/
public function create(array $attributes, Presenter $presenter): void
{
$tokenString = $this->tokens::generateTokenString();
$token = $this->tokens->create(
new Token(
0,
$this->castToInt($attributes['user_id']),
HashedSearchable::hash($attributes['salt'], $tokenString),
$attributes['name'] ?? null,
$this->castToDate($attributes['expires_at'] ?? null)
)
);
$response = new ResponseModel($tokenString, $token);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
interface CreateTokenUseCase
{
/**
* Required Attributes:
* user_id
* salt
* Optional Attributes:
* expires_at (Must be of format Y-m-d H:i:s)
* name
*
* @param array $attributes
* @param \Source\UseCases\Tokens\CreateToken\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function create(array $attributes, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class CreateTokenUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(CreateTokenUseCase::class, static function (Application $app) {
return new CreateToken($app->make(TokensRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [CreateTokenUseCase::class];
}
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Tokens\CreateToken;
use Source\Entities\Token;
class ResponseModel
{
/**
* @var string
*/
protected string $tokenString;
/**
* @var \Source\Entities\Token
*/
protected Token $token;
public function __construct(string $tokenString, Token $token)
{
$this->tokenString = $tokenString;
$this->token = $token;
}
/**
* @return string
*/
public function getTokenString(): string
{
return $this->tokenString;
}
/**
* @return \Source\Entities\Token
*/
public function getToken(): Token
{
return $this->token;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment