/** * * @return ErrorResponse|Response * @throws Exception */ protected function create() { $missing_fields = UserController::validateJSONFormat($this->body, User::REQUIRED_POST_FIELDS); // Check that required fields are not missing if (!$missing_fields) { $mapper = new UserDBMapper(); $user = User::fromJSON($this->body); if ($this->isValidEmail($user->getEmail())) { $db_response = $mapper->add($user); if ($db_response instanceof DBError) { $response = new ErrorResponse($db_response); } elseif (is_numeric($db_response)) { $this->id = $db_response; ResetPasswordController::setNewPassword($this->body, EmailSender::REGISTER_EMAIL); // Set random password and notify user by email $response = $this->get(); $response->setResponseCode(Response::STATUS_CODE_CREATED); } else { throw new Exception("Not implemented error"); } } else { $response = new ErrorResponse(new DuplicateUserError()); } } else { $response = new ErrorResponse(new MalformedJSONFormatError($missing_fields)); } return $response; }