Commit 5763737f authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Get use cases done for attempts read

parent 71ca1cb4
......@@ -9,10 +9,10 @@ use Source\Gateways\Doors\DoorsRepositoryServiceProvider;
use Source\Gateways\Users\UsersRepositoryServiceProvider;
use Source\Gateways\Groups\GroupsRepositoryServiceProvider;
use Source\Gateways\Tokens\TokensRepositoryServiceProvider;
use Source\UseCases\Door\Access\AccessUseCaseServiceProvider;
use Source\Gateways\Entries\EntriesRepositoryServiceProvider;
use Source\Gateways\DoorUser\DoorUserRepositoryServiceProvider;
use Source\UseCases\Door\Access\AccessUseCaseServiceProvider;
use Source\Gateways\Attempts\AttemptsRepositoryServiceProvider;
use Source\Gateways\DoorUser\DoorUserRepositoryServiceProvider;
use Source\UseCases\Doors\GetDoor\GetDoorUseCaseServiceProvider;
use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
use Source\Gateways\DoorGroup\DoorGroupRepositoryServiceProvider;
......@@ -34,6 +34,7 @@ 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\Attempts\GetAttempts\GetAttemptsUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
......@@ -44,8 +45,10 @@ use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttemptsForDoor\GetAttemptsForDoorUseCaseServiceProvider;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCaseServiceProvider;
use Source\UseCases\Door\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
use Source\UseCases\Users\Authenticate\AuthenticateUseCaseServiceProvider as UserAuthenticateUseCaseServiceProvider;
......@@ -121,6 +124,11 @@ class AppServiceProvider extends ServiceProvider
// TokenUser
GetUserTokensUseCaseServiceProvider::class,
// Attempts
GetAttemptsUseCaseServiceProvider::class,
GetAttemptsForDoorUseCaseServiceProvider::class,
GetAttemptCountForDoorUseCaseServiceProvider::class,
];
/**
......
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['count'] = $responseModel->getCount();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Carbon\Carbon;
use Source\Gateways\Attempts\AttemptsRepository;
class GetAttemptCountForDoor implements GetAttemptCountForDoorUseCase
{
/**
* @var \Source\Gateways\Attempts\AttemptsRepository
*/
protected AttemptsRepository $attempts;
public function __construct(AttemptsRepository $attempts)
{
$this->attempts = $attempts;
}
/**
* @inheritDoc
*/
public function count(string $doorId, string $begin, string $end, Presenter $presenter): void
{
$count = $this->attempts->getCountForDoorBetween($doorId, new Carbon($begin), new Carbon($end));
$responseModel = new ResponseModel($count);
$presenter->present($responseModel);
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
interface GetAttemptCountForDoorUseCase
{
/**
* Begin and end must be parsable strings by datetime
*
* @param string $doorId
* @param string $begin
* @param string $end
* @param \Source\UseCases\Attempts\GetAttemptCountForDoor\Presenter $presenter
* @throws \Exception
*/
public function count(string $doorId, string $begin, string $end, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetAttemptCountForDoorUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetAttemptCountForDoorUseCase::class, static function (Application $app) {
return new GetAttemptCountForDoor($app->make(AttemptsRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetAttemptCountForDoorUseCase::class];
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
class ResponseModel
{
/**
* @var int
*/
protected int $count;
public function __construct(int $count)
{
$this->count = $count;
}
/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
use Source\Entities\Attempt;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['attempts'] = array_map(function (Attempt $attempt) {
return $this->formatAttempt($attempt);
}, $responseModel->getAttempts());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
use Carbon\Carbon;
use Source\Gateways\Attempts\AttemptsRepository;
class GetAttempts implements GetAttemptsUseCase
{
/**
* @var \Source\Gateways\Attempts\AttemptsRepository
*/
protected AttemptsRepository $attempts;
public function __construct(AttemptsRepository $attempts)
{
$this->attempts = $attempts;
}
/**
* @inheritDoc
*/
public function getBetweenDates(string $start, string $end, Presenter $presenter): void
{
$attempts = $this->attempts->getBetween(new Carbon($start), new Carbon($end));
$response = new ResponseModel($attempts);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
interface GetAttemptsUseCase
{
/**
* the passed start and end dates must be parsable by datetime
*
* @param string $start
* @param string $end
* @param \Source\UseCases\Attempts\GetAttempts\Presenter $presenter
* @throws \Exception
*/
public function getBetweenDates(string $start, string $end, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetAttemptsUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetAttemptsUseCase::class, static function (Application $app) {
return new GetAttempts($app->make(AttemptsRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetAttemptsUseCase::class];
}
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Attempts\GetAttempts;
class ResponseModel
{
/**
* @var \Source\Entities\Attempt[]
*/
protected array $attempts;
/**
* @var \Source\Entities\Attempt[]
*/
public function __construct(array $attempts)
{
$this->attempts = $attempts;
}
/**
* @return \Source\Entities\Attempt[]
*/
public function getAttempts(): array
{
return $this->attempts;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
use Source\Entities\Attempt;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['attempts'] = array_map(function (Attempt $attempt) {
return $this->formatAttempt($attempt);
}, $responseModel->getAttempts());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
use Carbon\Carbon;
use Source\Gateways\Attempts\AttemptsRepository;
class GetAttemptsForDoor implements GetAttemptsForDoorUseCase
{
/**
* @var \Source\Gateways\Attempts\AttemptsRepository
*/
protected AttemptsRepository $attempts;
public function __construct(AttemptsRepository $attempts)
{
$this->attempts = $attempts;
}
/**
* @inheritDoc
*/
public function getAttemptsForDoorBetween(string $doorId, string $begin, string $end, Presenter $presenter): void
{
$attempts = $this->attempts->getForDoorBetween($doorId, new Carbon($begin), new Carbon($end));
$response = new ResponseModel($attempts);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
interface GetAttemptsForDoorUseCase
{
/**
* Begin and end must be parsable by datetime
*
* @param string $doorId
* @param string $begin
* @param string $end
* @param \Source\UseCases\Attempts\GetAttemptsForDoor\Presenter $presenter
* @throws \Exception
*/
public function getAttemptsForDoorBetween(string $doorId, string $begin, string $end, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetAttemptsForDoorUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetAttemptsForDoorUseCase::class, static function (Application $app) {
return new GetAttemptsForDoor($app->make(AttemptsRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetAttemptsForDoorUseCase::class];
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptsForDoor;
class ResponseModel
{
/**
* @var \Source\Entities\Attempt[]
*/
protected array $attempts;
/**
* @param \Source\Entities\Attempt[]
*/
public function __construct(array $attempts)
{
$this->attempts = $attempts;
}
/**
* @return \Source\Entities\Attempt[]
*/
public function getAttempts(): array
{
return $this->attempts;
}
}
......@@ -6,8 +6,10 @@ namespace Source\UseCases;
use Carbon\Carbon;
use Source\Entities\Door;
use Source\Entities\User;
use Source\Entities\Entry;
use Source\Entities\Group;
use Source\Entities\Token;
use Source\Entities\Attempt;
abstract class BasePresenter
{
......@@ -118,4 +120,36 @@ abstract class BasePresenter
'updated_at' => $this->formatDateTime($token->getUpdatedAt()),
];
}
/**
* @param \Source\Entities\Attempt|null $attempt
* @return array
*/
public function formatAttempt(?Attempt $attempt): array
{
if (!$attempt) {
return [];
}
return [
'id' => $attempt->getId(),
'door_id' => $attempt->getDoorId(),
'created_at' => $attempt->getCreatedAt(),
];
}
public function formatEntry(?Entry $entry): array
{
if (!$entry) {
return [];