2016-03-05 00:36:11 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 07:35:37 +02:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
2016-08-20 23:24:22 +02:00
|
|
|
DataValidator::with('CustomValidations', true);
|
2016-07-19 07:35:37 +02:00
|
|
|
|
2016-03-05 00:51:59 +01:00
|
|
|
class SignUpController extends Controller {
|
|
|
|
const PATH = '/signup';
|
2016-07-15 00:06:47 +02:00
|
|
|
|
|
|
|
private $userEmail;
|
2016-07-27 05:50:20 +02:00
|
|
|
private $userName;
|
2016-07-15 00:06:47 +02:00
|
|
|
private $userPassword;
|
2016-03-05 00:36:11 +01:00
|
|
|
|
2016-07-04 20:57:00 +02:00
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'any',
|
2016-07-19 07:35:37 +02:00
|
|
|
'requestData' => [
|
|
|
|
'name' => [
|
2016-07-19 21:41:04 +02:00
|
|
|
'validation' => DataValidator::length(2, 55)->alpha(),
|
2016-07-19 07:35:37 +02:00
|
|
|
'error' => ERRORS::INVALID_NAME
|
|
|
|
],
|
|
|
|
'email' => [
|
2016-07-19 21:41:04 +02:00
|
|
|
'validation' => DataValidator::email(),
|
2016-07-19 07:35:37 +02:00
|
|
|
'error' => ERRORS::INVALID_EMAIL
|
|
|
|
],
|
|
|
|
'password' => [
|
2016-07-19 21:41:04 +02:00
|
|
|
'validation' => DataValidator::length(5, 200),
|
2016-07-19 07:35:37 +02:00
|
|
|
'error' => ERRORS::INVALID_PASSWORD
|
2016-08-20 23:24:22 +02:00
|
|
|
],
|
|
|
|
'captcha' => [
|
|
|
|
'validation' => DataValidator::captcha(),
|
|
|
|
'error' => ERRORS::INVALID_CAPTCHA
|
2016-07-19 07:35:37 +02:00
|
|
|
]
|
|
|
|
]
|
2016-07-04 20:57:00 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-04 09:09:32 +02:00
|
|
|
|
2016-03-05 00:51:59 +01:00
|
|
|
public function handler() {
|
2016-07-27 05:50:20 +02:00
|
|
|
$this->storeRequestData();
|
2016-03-05 00:51:59 +01:00
|
|
|
|
2016-09-09 07:24:12 +02:00
|
|
|
$existentUser = User::getUser($this->userEmail, 'email');
|
2016-03-05 00:51:59 +01:00
|
|
|
|
2016-09-09 07:24:12 +02:00
|
|
|
if (!$existentUser->isNull()) {
|
|
|
|
Response::respondError(ERRORS::USER_EXISTS);
|
|
|
|
return;
|
2016-07-15 00:06:47 +02:00
|
|
|
}
|
2016-03-20 19:45:40 +01:00
|
|
|
|
2016-09-09 07:24:12 +02:00
|
|
|
$userId = $this->createNewUserAndRetrieveId();
|
|
|
|
$this->sendRegistrationMail();
|
|
|
|
|
|
|
|
Response::respondSuccess([
|
|
|
|
'userId' => $userId,
|
|
|
|
'userEmail' => $this->userEmail
|
|
|
|
]);
|
|
|
|
|
2016-07-15 00:06:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 05:50:20 +02:00
|
|
|
public function storeRequestData() {
|
|
|
|
$this->userName = Controller::request('name');
|
2016-07-15 00:06:47 +02:00
|
|
|
$this->userEmail = Controller::request('email');
|
|
|
|
$this->userPassword = Controller::request('password');
|
2016-03-20 19:45:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 00:06:47 +02:00
|
|
|
public function createNewUserAndRetrieveId() {
|
2016-03-05 00:51:59 +01:00
|
|
|
$userInstance = new User();
|
2016-07-15 00:06:47 +02:00
|
|
|
|
|
|
|
$userInstance->setProperties([
|
2016-07-27 05:50:20 +02:00
|
|
|
'name' => $this->userName,
|
2016-11-30 07:33:46 +01:00
|
|
|
'signupDate' => Date::getCurrentDate(),
|
|
|
|
'tickets' => 0,
|
2016-07-15 00:06:47 +02:00
|
|
|
'email' => $this->userEmail,
|
|
|
|
'password' => Hashing::hashPassword($this->userPassword)
|
|
|
|
]);
|
2016-03-05 00:51:59 +01:00
|
|
|
|
2016-03-20 19:45:40 +01:00
|
|
|
return $userInstance->store();
|
2016-03-05 00:51:59 +01:00
|
|
|
}
|
2016-07-15 00:06:47 +02:00
|
|
|
|
|
|
|
public function sendRegistrationMail() {
|
|
|
|
$mailSender = new MailSender();
|
|
|
|
|
|
|
|
$mailSender->setTemplate(MailTemplate::USER_SIGNUP, [
|
2016-07-27 05:50:20 +02:00
|
|
|
'to' => $this->userEmail,
|
|
|
|
'name' => $this->userName
|
2016-07-15 00:06:47 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$mailSender->send();
|
|
|
|
}
|
2016-03-05 00:51:59 +01:00
|
|
|
}
|