Skip to content

Instantly share code, notes, and snippets.

@Kyslik
Created November 20, 2017 12:23
Show Gist options
  • Save Kyslik/46eaa61c4f9a49b09935eebe7fa5b7be to your computer and use it in GitHub Desktop.
Save Kyslik/46eaa61c4f9a49b09935eebe7fa5b7be to your computer and use it in GitHub Desktop.
Example of not using @call method, no namespaces and no imports.
<?php
class CheckIfUserIsConfirmedTask extends Task
{
private $user;
public function run($email, $password)
{
$this->loginWithCredentials($email, $password);
// is the config flag set?
if (Config::get('authentication-container.require_email_confirmation')) {
if ( ! $this->user) {
throw new LoginFailedException();
}
if ( ! $this->user->confirmed) {
throw new UserNotConfirmedException();
}
}
}
public function loginWithCredentials($email, $password)
{
if (Auth::attempt(['email' => $email, 'password' => $password])) {
$this->user = Auth::user();
} else {
throw new LoginFailedException();
}
}
public function setUser(User $user)
{
$this->user = $user;
}
}
<?php
/**
* Class ProxyApiLoginAction.
*/
class ProxyApiLoginAction extends Action
{
/**
* @var \App\Containers\Authentication\Tasks\CallOAuthServerTask
*/
private $authServerTask;
/**
* @var \App\Containers\Authentication\Tasks\CheckIfUserIsConfirmedTask
*/
private $isConfirmedTask;
/**
* @var \App\Containers\Authentication\Tasks\MakeRefreshCookieTask
*/
private $refreshCookieTask;
/**
* ProxyApiLoginAction constructor.
*
* @param \App\Containers\Authentication\Tasks\CallOAuthServerTask $authServerTask
* @param \App\Containers\Authentication\Tasks\CheckIfUserIsConfirmedTask $isConfirmedTask
* @param \App\Containers\Authentication\Tasks\MakeRefreshCookieTask $refreshCookieTask
*/
public function __construct(
CallOAuthServerTask $authServerTask,
CheckIfUserIsConfirmedTask $isConfirmedTask,
MakeRefreshCookieTask $refreshCookieTask
) {
$this->authServerTask = $authServerTask;
$this->isConfirmedTask = $isConfirmedTask;
$this->refreshCookieTask = $refreshCookieTask;
}
/**
* @param \App\Ship\Parents\Requests\Request $request
* @param $clientId
* @param $clientPassword
*
* @return array
*/
public function run(Request $request, $clientId, $clientPassword)
{
$authData = [
'grant_type' => 'password',
'client_id' => $clientId,
'client_secret' => $clientPassword,
'username' => $request->email,
'password' => $request->password,
'scope' => '',
];
$responseContent = $this->authServerTask->run($authData);
$this->isConfirmedTask->run($request->email, $request->password);
$refreshCookie = $this->refreshCookieTask->run($responseContent['refresh_token']);
return [
'response-content' => $responseContent,
'refresh-cookie' => $refreshCookie,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment