/** * @param RegisterBindingModel $model * @throws \Exception * @POST */ public function register(RegisterBindingModel $model) { $model->setCash(self::DEFAULT_USER_CASH); $registerResult = $this->eshopData->getUsersRepository()->create($model); if ($registerResult) { $data = ["username" => $model->getUsername(), "password" => $model->getPassword()]; $loginDetails = new LoginBindingModel($data); $this->login($loginDetails); } throw new \Exception("Registration error"); }
public function create(RegisterBindingModel $model) { if ($model->getPassword() != $model->getConfirmPassword()) { throw new \Exception('Passwords does not match'); } $this->db->beginTransaction(); $statement = $this->db->prepare("\n INSERT INTO users (username, password_hash, email, register_date, cash)\n VALUE (?, ?, ?, NOW(), ?)\n "); $data = [$model->getUsername(), password_hash($model->getPassword(), AppConfig::PASSWORD_HASH_ALGORITHM), $model->getEmail(), $model->getCash()]; if (!$statement->execute($data)) { echo $statement->errorInfo(); $this->db->rollBack(); return false; } $registeredUser = $this->findByUsername($model->getUsername()); // Inserting user role $statement = $this->db->prepare("\n INSERT INTO user_roles (user_id, role_id)\n VALUES (?, ?)\n "); if (!$statement->execute([$registeredUser->getId(), App::$roles[AppConfig::DEFAULT_USER_ROLE]])) { echo $statement->errorInfo(); $this->db->rollBack(); return false; } //making the user cart $statement = $this->db->prepare("\n INSERT INTO usercart (user_id)\n VALUES (?)\n "); if (!$statement->execute([$registeredUser->getId()])) { echo $statement->errorInfo(); $this->db->rollBack(); return false; } $this->db->commit(); return true; }