Guest User

Untitled

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