mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim.git
synced 2026-08-19 04:24:00 +00:00
73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\ControllersLogic\Account;
|
|
|
|
|
|
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
|
|
use App\Classes\Modules\Accounts\Exceptions\CannotCreateUserException;
|
|
use App\Classes\Modules\Accounts\Services\CreatesUserAccount;
|
|
use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
|
|
use App\Classes\Modules\ControllerLogic\Exceptions\MalformedRequestException;
|
|
use App\Classes\ValueObjects\Constants\HttpStatus;
|
|
use App\Events\Accounts\UserHasRegistered;
|
|
use App\Models\User;
|
|
use App\Traits\DeterminesIfUserPasswordMatchesRequirements;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class RegisterAccountLogic
|
|
{
|
|
|
|
use DeterminesIfUserPasswordMatchesRequirements;
|
|
|
|
/** @var User */
|
|
private $repository;
|
|
|
|
/** @var CreatesUserAccount */
|
|
private $createsUserAccount;
|
|
|
|
/**
|
|
* RegisterAccountLogic constructor.
|
|
*
|
|
* @param User $repository
|
|
* @param CreatesUserAccount $createsUserAccount
|
|
*/
|
|
public function __construct(
|
|
User $repository,
|
|
CreatesUserAccount $createsUserAccount
|
|
) {
|
|
$this->repository = $repository;
|
|
$this->createsUserAccount = $createsUserAccount;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws InternalServerErrorException
|
|
* @throws MalformedRequestException
|
|
*/
|
|
public function execute(Request $request) {
|
|
|
|
// throws malformed request exception if the password is invalid
|
|
if ($this->checkPasswordAndThrowIfInvalid($request->get('password')) === false) {
|
|
throw new MalformedRequestException('Password strength is insufficient');
|
|
}
|
|
|
|
try {
|
|
|
|
// the email doesn't exist in the database, so create the user
|
|
$object = new UserObject($request->get('first_name'), $request->get('last_name'),
|
|
$request->get('email'), $request->get('password'), false);
|
|
|
|
$user = $this->createsUserAccount->execute($object);
|
|
|
|
// dispatch event for user registered
|
|
event(new UserHasRegistered($user));
|
|
|
|
return new JsonResponse(null, HttpStatus::RESOURCE_CREATED);
|
|
|
|
} catch (CannotCreateUserException $exception) {
|
|
throw new InternalServerErrorException("Failed to create user because {$exception->getMessage()}");
|
|
}
|
|
}
|
|
} |