Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. if($_POST)
  2. {
  3. try{
  4.  
  5. $input['name'] = $_POST['name'];
  6. $input['email'] = $_POST['email'];
  7. $input['username'] = $_POST['username'];
  8. $input['password'] = $_POST['password'];
  9.  
  10. $user_register_obj = new UserRegister(new UserInfoRepository());
  11. $user_register_obj->register($input);
  12.  
  13. header('Location: login.php');
  14. exit();
  15. }
  16. catch(CustomException $e)
  17. {
  18. var_dump($e->getCustomMessage());
  19. }
  20. }
  21.  
  22. ?>
  23.  
  24. interface IUserInfo {
  25.  
  26. public function getId();
  27. public function getName();
  28. public function getEmail();
  29. public function getUserName();
  30. public function getCreatedAt();
  31. public function getUpdatedAt();
  32. public function getPassword();
  33.  
  34. }
  35.  
  36. class User implements IUserInfo{
  37.  
  38. private $id;
  39. private $name;
  40. private $email;
  41. private $username;
  42. private $created_at;
  43. private $updated_at;
  44. private $password;
  45.  
  46. public function __construct($id,
  47. $name,
  48. $email,
  49. $username,
  50. $created_at,
  51. $updated_at,
  52. $password)
  53. {
  54. $this->id = $id;
  55. $this->name = $name;
  56. $this->email = $email;
  57. $this->username = $username;
  58. $this->created_at = $created_at;
  59. $this->updated_at = $updated_at;
  60. $this->password = $password;
  61. }
  62. public function getEmail()
  63. {
  64. return $this->email;
  65. }
  66.  
  67. public function getId()
  68. {
  69. return $this->id;
  70. }
  71.  
  72. public function getName()
  73. {
  74. return $this->name;
  75. }
  76.  
  77. public function getUserName()
  78. {
  79. return $this->username;
  80. }
  81.  
  82. public function getCreatedAt()
  83. {
  84. return $this->created_at;
  85. }
  86.  
  87. public function getUpdatedAt()
  88. {
  89. return $this->updated_at;
  90. }
  91. public function getPassword()
  92. {
  93. return $this->password;
  94. }
  95. }
  96.  
  97. interface IUserInfoRepository {
  98.  
  99. public function getById($id);
  100. public function getByEmail($email);
  101. public function getAll();
  102. public function insert($input);
  103. public function update($id, $input);
  104. public function delete($id);
  105.  
  106. }
  107.  
  108. class UserInfoRepository implements IUserInfoRepository{
  109.  
  110. private $table = 'users';
  111. private $db_obj;
  112.  
  113. public function __construct()
  114. {
  115. $this->db_obj = new DatabaseHandlerDatabase();
  116. }
  117.  
  118. public function getById($id)
  119. {
  120. $query = 'select * from users where id = ' . $id;
  121.  
  122. return $this->db_obj->GetOneRow($query);
  123. }
  124.  
  125. public function getAll()
  126. {
  127. // TODO: Implement getAll() method.
  128. }
  129.  
  130. public function getByEmail($email)
  131. {
  132. $query = "select * from user where email = '$email'";
  133.  
  134. $obj = $this->db_obj->GetOneRow($query);
  135.  
  136. if(!$obj)
  137. {
  138. return null;
  139. }
  140. else
  141. {
  142. return new User($obj['id'], $obj['name'], $obj['email'], $obj['username'], $obj['created_at'], $obj['updated_at'], $obj['password']);
  143. }
  144. }
  145.  
  146. public function insert($input)
  147. {
  148. $name = $input['name'];
  149. $email = $input['email'];
  150. $username = $input['username'];
  151. $password = md5($input['password']);
  152.  
  153. $query = "insert into user (name, email, username, password) values ('$name', '$email', '$username', '$password' )";
  154. return $this->db_obj->InsertAndGetId($query);
  155. }
  156.  
  157. public function update($id, $input)
  158. {
  159. // TODO: Implement update() method.
  160. }
  161.  
  162. public function delete($id)
  163. {
  164. // TODO: Implement delete() method.
  165. }
  166. }
  167.  
  168. class UserRegister {
  169.  
  170. private $userRepo;
  171.  
  172. public function __construct(IUserInfoRepository $userRepo)
  173. {
  174. $this->userRepo = $userRepo;
  175. }
  176.  
  177. public function register($input)
  178. {
  179. $email = $input['email'];
  180.  
  181. $result = filter_var( $email , FILTER_VALIDATE_EMAIL );
  182. if(!$result)
  183. throw new CustomException("Invalid Email");
  184.  
  185. $obj = $this->userRepo->getByEmail($email);
  186. if(!is_null($obj))
  187. throw new CustomException("Email already found");
  188.  
  189. $this->userRepo->insert($input);
  190.  
  191. return true;
  192. }
  193. }
  194.  
  195. public function register($input)
  196. {
  197. $email = $input['email'];
  198.  
  199. $result = filter_var( $email , FILTER_VALIDATE_EMAIL );
  200. if(!$result)
  201. throw new CustomException("Invalid Email");
  202.  
  203. $obj = $this->userRepo->getByEmail($email);
  204. if(!is_null($obj))
  205. throw new CustomException("Email already found");
  206.  
  207. $this->userRepo->insert($input);
  208.  
  209. return true;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement