Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Guardians of the Kretschmar Elock System
Doorcode
Commits
33b912e0
Commit
33b912e0
authored
Apr 08, 2020
by
Jacob Priddy
👌
Browse files
Add token create use case
parent
93a25fed
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/web/backend/src/Sanitize/CastsTo.php
View file @
33b912e0
...
...
@@ -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
;
}
}
src/web/backend/src/UseCases/BasePresenter.php
View file @
33b912e0
...
...
@@ -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
()),
];
}
}
src/web/backend/src/UseCases/Tokens/CreateToken/APIPresenter.php
0 → 100644
View file @
33b912e0
<?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
;
}
}
src/web/backend/src/UseCases/Tokens/CreateToken/CreateToken.php
0 → 100644
View file @
33b912e0
<?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
);
}
}
src/web/backend/src/UseCases/Tokens/CreateToken/CreateTokenUseCase.php
0 → 100644
View file @
33b912e0
<?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
;
}
src/web/backend/src/UseCases/Tokens/CreateToken/CreateTokenUseCaseServiceProvider.php
0 → 100644
View file @
33b912e0
<?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
];
}
}
src/web/backend/src/UseCases/Tokens/CreateToken/Presenter.php
0 → 100644
View file @
33b912e0
<?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
;
}
src/web/backend/src/UseCases/Tokens/CreateToken/ResponseModel.php
0 → 100644
View file @
33b912e0
<?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
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment