/** * Registers a user with the details within the HTTP Request object if no user currently exists * with a matching username or email address. * * @param Request $request The HTTP Request object. * @param Response $response The HTTP Response object. * @param array $args The array containing arguments provided. * * @return string The message from the registration process. */ public function register(Request $request, Response $response, array $args) { //get post variables from request body $post = $request->getParams(); //validate post variables (exist, and as expected) /** @var Validator $v */ $v = new Validator($post); $v->rule('required', ['username', 'password', 'email', 'first_name', 'last_name', 'date_of_birth']); $v->rule('email', 'email'); $ret = array(); if ($v->validate()) { if ($this->dbService->userExists($post['username'], $post['email'])) { $ret['message'] = "User already exists."; $ret['success'] = false; } else { if ($key = $this->dbService->addNewUser($post) ?: false) { $this->emailService->sendVerificationEmail($post['email'], $post['first_name'], $post['last_name'], $key); $ret['message'] = "You are now registered! A confirmation email has been sent to you. Please open it and follow\r\n the instructions provided."; $ret['success'] = true; } else { $ret['message'] = "Something went wrong. Please try again later."; $ret['success'] = false; } } } else { $ret['message'] = "Please complete all fields."; $ret['success'] = false; } return json_encode($ret); }
/** * @depends testCanAddNewUser * @param array $data * @return mixed */ public function testUserExistsAfterInsert(array $data) { $this->assertTrue($this->dbService->userExists($data['username'])); return $data; }