Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if($_POST)
- {
- try{
- $input['name'] = $_POST['name'];
- $input['email'] = $_POST['email'];
- $input['username'] = $_POST['username'];
- $input['password'] = $_POST['password'];
- $user_register_obj = new UserRegister(new UserInfoRepository());
- $user_register_obj->register($input);
- header('Location: login.php');
- exit();
- }
- catch(CustomException $e)
- {
- var_dump($e->getCustomMessage());
- }
- }
- ?>
- interface IUserInfo {
- public function getId();
- public function getName();
- public function getEmail();
- public function getUserName();
- public function getCreatedAt();
- public function getUpdatedAt();
- public function getPassword();
- }
- class User implements IUserInfo{
- private $id;
- private $name;
- private $email;
- private $username;
- private $created_at;
- private $updated_at;
- private $password;
- public function __construct($id,
- $name,
- $email,
- $username,
- $created_at,
- $updated_at,
- $password)
- {
- $this->id = $id;
- $this->name = $name;
- $this->email = $email;
- $this->username = $username;
- $this->created_at = $created_at;
- $this->updated_at = $updated_at;
- $this->password = $password;
- }
- public function getEmail()
- {
- return $this->email;
- }
- public function getId()
- {
- return $this->id;
- }
- public function getName()
- {
- return $this->name;
- }
- public function getUserName()
- {
- return $this->username;
- }
- public function getCreatedAt()
- {
- return $this->created_at;
- }
- public function getUpdatedAt()
- {
- return $this->updated_at;
- }
- public function getPassword()
- {
- return $this->password;
- }
- }
- interface IUserInfoRepository {
- public function getById($id);
- public function getByEmail($email);
- public function getAll();
- public function insert($input);
- public function update($id, $input);
- public function delete($id);
- }
- class UserInfoRepository implements IUserInfoRepository{
- private $table = 'users';
- private $db_obj;
- public function __construct()
- {
- $this->db_obj = new DatabaseHandlerDatabase();
- }
- public function getById($id)
- {
- $query = 'select * from users where id = ' . $id;
- return $this->db_obj->GetOneRow($query);
- }
- public function getAll()
- {
- // TODO: Implement getAll() method.
- }
- public function getByEmail($email)
- {
- $query = "select * from user where email = '$email'";
- $obj = $this->db_obj->GetOneRow($query);
- if(!$obj)
- {
- return null;
- }
- else
- {
- return new User($obj['id'], $obj['name'], $obj['email'], $obj['username'], $obj['created_at'], $obj['updated_at'], $obj['password']);
- }
- }
- public function insert($input)
- {
- $name = $input['name'];
- $email = $input['email'];
- $username = $input['username'];
- $password = md5($input['password']);
- $query = "insert into user (name, email, username, password) values ('$name', '$email', '$username', '$password' )";
- return $this->db_obj->InsertAndGetId($query);
- }
- public function update($id, $input)
- {
- // TODO: Implement update() method.
- }
- public function delete($id)
- {
- // TODO: Implement delete() method.
- }
- }
- class UserRegister {
- private $userRepo;
- public function __construct(IUserInfoRepository $userRepo)
- {
- $this->userRepo = $userRepo;
- }
- public function register($input)
- {
- $email = $input['email'];
- $result = filter_var( $email , FILTER_VALIDATE_EMAIL );
- if(!$result)
- throw new CustomException("Invalid Email");
- $obj = $this->userRepo->getByEmail($email);
- if(!is_null($obj))
- throw new CustomException("Email already found");
- $this->userRepo->insert($input);
- return true;
- }
- }
- public function register($input)
- {
- $email = $input['email'];
- $result = filter_var( $email , FILTER_VALIDATE_EMAIL );
- if(!$result)
- throw new CustomException("Invalid Email");
- $obj = $this->userRepo->getByEmail($email);
- if(!is_null($obj))
- throw new CustomException("Email already found");
- $this->userRepo->insert($input);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement