Guest User

Untitled

a guest
Dec 10th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. namespace Model;
  2.  
  3. abstract class AddUserTransation extends Transation
  4. {
  5.  
  6. private $name;
  7.  
  8. private $email;
  9.  
  10. private $password;
  11.  
  12. private $phone;
  13.  
  14. private $addRess;
  15.  
  16. protected const nameStrlenMin = 4;
  17.  
  18. protected const nameStrlenMax = 120;
  19.  
  20. protected const phoneStrlenMin = 10;
  21.  
  22. protected const phoneStrlenMax = 20;
  23.  
  24. private const passwordStrlenMin = 6;
  25.  
  26. private const passwordStrlenMax = 30;
  27.  
  28.  
  29. public function __construct(DataBase $dataBase, string $name, string $email, string $password, int $phone, string $addRess)
  30. {
  31. parent::__construct($dataBase);
  32. $this->validateName($name);
  33. $this->validateEmail($email);
  34. $this->validatePassword($password);
  35. $this->validatePhone($phone);
  36. $this->validateAddRess($addRess);
  37. }
  38.  
  39. public function execute( ): void
  40. {
  41. if ($this->emailAlreadyExist()) throw new \Exception('emailAlreadyExist');
  42.  
  43. if ($this->phoneAlreadyExist()) throw new \Exception('phoneAlreadyExist');
  44.  
  45. $User = new User($this->name, $this->email, $this->password, $this->phone, $this->addRess);
  46.  
  47. $User->type = $this->makeType();
  48.  
  49. $this->DataBase->addUser($User);
  50. }
  51.  
  52. protected abstract function makeType( );
  53.  
  54. private function emailAlreadyExist( ): bool
  55. {
  56. $result = $this->DataBase->getUserByEmail($this->email);
  57.  
  58. if ($this->validate->isObjectNullData($result)) return FALSE;
  59.  
  60. return TRUE;
  61. }
  62.  
  63. private function phoneAlreadyExist( ): bool
  64. {
  65. $result = $this->DataBase->phoneAlreadyExist($this->phone);
  66.  
  67. if ($this->validate->isObjectNullData($result)) return FALSE;
  68.  
  69. return TRUE;
  70. }
  71.  
  72. private function validateName(string $name): void
  73. {
  74. if (! $this->validate->onlyStringAndSpace($name) || ! $this->validate->strlenMinAndMax(self::nameStrlenMin,
  75. self::nameStrlenMax, $name))
  76. {
  77. throw new \InvalidArgumentException('nameInvalid');
  78. }
  79. $this->name = $name;
  80. }
  81.  
  82. private function validateEmail(string $email): void
  83. {
  84. if (! $this->validate->isEmail($email)) throw new \InvalidArgumentException('emailInvalid');
  85.  
  86. $this->email = strtolower($email);
  87. }
  88.  
  89. private function validatePassword(string $password): void
  90. {
  91. if (! $this->validate->onlyStringNumberUnderlineAndDot($password) ||
  92. ! $this->validate->strlenMinAndMax(self::passwordStrlenMin, self::passwordStrlenMax, $password))
  93. {
  94. throw new \InvalidArgumentException('passwordInvalid');
  95. }
  96. $this->password = $password;
  97. }
  98.  
  99. private function validatePhone(int $phone): void
  100. {
  101. if (! $this->validate->strlenMinAndMax(self::phoneStrlenMin, self::phoneStrlenMax, $phone))
  102. {
  103. throw new \InvalidArgumentException('phoneInvalid');
  104. }
  105. $this->phone = $phone;
  106. }
  107.  
  108. private function validateAddRess(string $addRess): void
  109. {
  110. if (! $this->validate->onlyStringAndSpace($addRess) || ! $this->validate->strlenMax(200, $addRess))
  111. {
  112. throw new \InvalidArgumentException('addRessInvalid');
  113. }
  114. $this->addRess = $addRess;
  115. }
Add Comment
Please, Sign In to add comment