/** * @param RegisterInputModel $model * @return mixed * @Validatetoken('token') * @Method('GET', 'POST') */ public function register(RegisterInputModel $model) : View { if (!$model->isValid()) { return new View('account', 'register', $model); } $service = new AccountService($this->dbContext); if (HttpContext::getInstance()->isPost()) { $result = $service->register($model); if (!$result->hasError()) { $this->addInfoMessage('Registration was successful.'); $this->redirect('home', 'index'); } else { $this->addErrorMessage('Registration failed.'); $this->redirect('account', 'register'); } } else { return new View('account', 'register', new RegisterInputModel()); } }
/** * @param RegisterInputModel $model * @return ServiceResponse */ public function register(RegisterInputModel $model) : ServiceResponse { $user = new User($model->getUsername(), $model->getEmail(), password_hash($model->getPassword(), PASSWORD_DEFAULT), $model->getFullname(), $model->getTelephone()); $this->dbContext->getUsersRepository()->add($user); $this->dbContext->saveChanges(); $_SESSION['userId'] = $user->getId(); $_SESSION['username'] = $user->getUsername(); return new ServiceResponse(); }