Commit 466bcf16 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Get all tokens and get token use cases

parent 00ac82cc
......@@ -16,6 +16,7 @@ use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
use Source\Gateways\DoorGroup\DoorGroupRepositoryServiceProvider;
use Source\Gateways\GroupUser\GroupUserRepositoryServiceProvider;
use Source\UseCases\Groups\GetGroup\GetGroupUseCaseServiceProvider;
use Source\UseCases\Tokens\GetToken\GetTokenUseCaseServiceProvider;
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCaseServiceProvider;
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCaseServiceProvider;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCaseServiceProvider;
......@@ -29,8 +30,10 @@ use Source\UseCases\Groups\DeleteGroup\DeleteGroupUseCaseServiceProvider;
use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCaseServiceProvider;
use Source\UseCases\Tokens\CreateToken\CreateTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCaseServiceProvider;
use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
......@@ -105,8 +108,11 @@ class AppServiceProvider extends ServiceProvider
GenerateDoorTokenUseCaseServiceProvider::class,
// Tokens
GetTokenUseCaseServiceProvider::class,
CreateTokenUseCaseServiceProvider::class,
ExpireTokenUseCaseServiceProvider::class,
UpdateTokenUseCaseServiceProvider::class,
GetAllTokensUseCaseServiceProvider::class,
];
/**
......
......@@ -33,14 +33,47 @@ class DatabaseTokensRepository implements TokensRepository
$dbToken = $user->tokens()->save($dbToken);
return $this->dbTokenToToken($dbToken);
return self::dbTokenToToken($dbToken);
}
/**
* @inheritDoc
*/
public function update(string $tokenId, Token $token): Token
{
$dbToken = \App\Token::find($this->castToInt($tokenId));
if (!$dbToken) {
throw new EntityNotFoundException();
}
$dbToken->name = $token->getName();
$dbToken->expired_at = $token->getExpiresAt();
$dbToken->api_token = $token->getTokenString();
$dbToken->save();
return self::dbTokenToToken($dbToken);
}
/**
* @inheritDoc
*/
public function get(string $tokenId): ?Token
{
$token = \App\Token::find($this->castToInt($tokenId));
if (!$token) {
return null;
}
return self::dbTokenToToken($token);
}
/**
* @param \App\Token $token
* @return \Source\Entities\Token
*/
protected function dbTokenToToken(\App\Token $token): Token
protected static function dbTokenToToken(\App\Token $token): Token
{
return new Token(
$token->id,
......@@ -69,7 +102,7 @@ class DatabaseTokensRepository implements TokensRepository
return null;
}
return $this->dbTokenToToken($found);
return self::dbTokenToToken($found);
}
/**
......@@ -108,11 +141,23 @@ class DatabaseTokensRepository implements TokensRepository
throw new EntityNotFoundException();
}
$token = $this->dbTokenToToken($dbToken);
$token = self::dbTokenToToken($dbToken);
if ($token->isValid()) {
$token->expires_at = Carbon::now();
$token->save();
}
}
/**
* @inheritDoc
*/
public function all(): array
{
$tokens = \App\Token::all();
return array_map(static function (\App\Token $token) {
return self::dbTokenToToken($token);
}, $tokens);
}
}
......@@ -5,6 +5,7 @@ namespace Source\Gateways\Tokens;
use Carbon\Carbon;
use Source\Entities\Token;
use Source\Exceptions\EntityNotFoundException;
class InMemoryTokensRepository implements TokensRepository
{
......@@ -24,6 +25,22 @@ class InMemoryTokensRepository implements TokensRepository
return $token;
}
/**
* @inheritDoc
*/
public function update(string $tokenId, Token $token): Token
{
foreach ($this->tokens as $key=>$tok) {
if ($tok->hasIdOf($tokenId)) {
$this->tokens[$key] = $token;
return $token;
}
}
throw new EntityNotFoundException();
}
/**
* @return Token[]
*/
......@@ -78,4 +95,18 @@ class InMemoryTokensRepository implements TokensRepository
}
}
}
/**
* @inheritDoc
*/
public function get(string $tokenId): ?Token
{
foreach ($this->tokens as $token) {
if ($token->hasIdOf($tokenId)) {
return $token;
}
}
return null;
}
}
......@@ -15,6 +15,25 @@ interface TokensRepository
*/
public function create(Token $token): Token;
/**
* @param string $tokenId
* @param \Source\Entities\Token $token
* @return \Source\Entities\Token
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function update(string $tokenId, Token $token): Token;
/**
* @param string $tokenId
* @return \Source\Entities\Token|null
*/
public function get(string $tokenId): ?Token;
/**
* @return \Source\Entities\Token[]
*/
public function all(): array;
/**
* @param string $token
* @return Token|null
......
......@@ -7,11 +7,11 @@ interface CreateTokenUseCase
{
/**
* Required Attributes:
* user_id
* salt
* - user_id
* - salt
* Optional Attributes:
* expires_at (Must be of format Y-m-d H:i:s)
* name
* - expires_at (Must be of format Y-m-d H:i:s)
* - name
*
* @param array $attributes
* @param \Source\UseCases\Tokens\CreateToken\Presenter $presenter
......
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
use Source\Entities\Token;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['tokens'] = array_map(function (Token $token) {
return $this->formatToken($token);
}, $responseModel->getTokens());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
use Source\Gateways\Tokens\TokensRepository;
class GetAllTokens implements GetAllTokensUseCase
{
/**
* @var \Source\Gateways\Tokens\TokensRepository
*/
protected TokensRepository $tokens;
public function __construct(TokensRepository $tokens)
{
$this->tokens = $tokens;
}
/**
* @inheritDoc
*/
public function all(Presenter $presenter): void
{
$tokens = $this->tokens->all();
$response = new ResponseModel($tokens);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
interface GetAllTokensUseCase
{
/**
* @param \Source\UseCases\Tokens\GetAllTokens\Presenter $presenter
*/
public function all(Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
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 GetAllTokensUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetAllTokensUseCase::class, static function (Application $app) {
return new GetAllTokens($app->make(TokensRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetAllTokensUseCase::class];
}
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
class ResponseModel
{
/**
* @var \Source\Entities\Token[]
*/
protected array $tokens;
/**
* @param \Source\Entities\Token[] $tokens
*/
public function __construct(array $tokens)
{
$this->tokens = $tokens;
}
/**
* @return \Source\Entities\Token[]
*/
public function getTokens(): array
{
return $this->tokens;
}
}
<?php
namespace Source\UseCases\Tokens\GetToken;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['token'] = $responseModel->getToken();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Tokens\GetToken;
use Source\Gateways\Tokens\TokensRepository;
use Source\Exceptions\EntityNotFoundException;
class GetToken implements GetTokenUseCase
{
/**
* @var \Source\Gateways\Tokens\TokensRepository
*/
protected TokensRepository $tokens;
public function __construct(TokensRepository $tokens)
{
$this->tokens = $tokens;
}
/**
* @inheritDoc
*/
public function getToken(string $tokenId, Presenter $presenter): void
{
$token = $this->tokens->get($tokenId);
if (!$token) {
throw new EntityNotFoundException();
}
$response = new ResponseModel($token);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Tokens\GetToken;
interface GetTokenUseCase
{
/**
* @param string $tokenId
* @param \Source\UseCases\Tokens\GetToken\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getToken(string $tokenId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Tokens\GetToken;
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 GetTokenUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetTokenUseCase::class, static function (Application $app) {
return new GetToken($app->make(TokensRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetTokenUseCase::class];
}
}
<?php
namespace Source\UseCases\Tokens\GetToken;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Tokens\GetToken;
use Source\Entities\Token;
class ResponseModel
{
/**
* @var \Source\Entities\Token
*/
protected Token $token;
public function __construct(Token $token)
{
$this->token = $token;
}
/**
* @return \Source\Entities\Token
*/
public function getToken(): Token
{
return $this->token;
}
}
<?php
namespace Source\UseCases\Tokens\UpdateToken;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['token'] = $this->formatToken($responseModel->getToken());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Tokens\UpdateToken;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Tokens\UpdateToken;
use Source\Entities\Token;
class ResponseModel
{
/**
* @var \Source\Entities\Token
*/
protected Token $token;
public function __construct(Token $token)
{