Guest User

Untitled

a guest
Jan 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\User;
  6. use Illuminate\Console\Command;
  7.  
  8. class RegisterSuperAdminCommand extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'register:super-admin';
  16.  
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Register super admin';
  23.  
  24. /**
  25. * User model.
  26. *
  27. * @var object
  28. */
  29. private $user;
  30.  
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(User $user)
  37. {
  38. parent::__construct();
  39.  
  40. $this->user = $user;
  41. }
  42.  
  43. /**
  44. * Execute the console command.
  45. *
  46. * @return mixed
  47. */
  48. public function handle()
  49. {
  50. $details = $this->getDetails();
  51.  
  52. $admin = $this->user->createSuperAdmin($details);
  53.  
  54. $this->display($admin);
  55. }
  56.  
  57. /**
  58. * Ask for admin details.
  59. *
  60. * @return array
  61. */
  62. private function getDetails() : array
  63. {
  64. $details['name'] = $this->ask('Name');
  65. $details['email'] = $this->ask('Email');
  66. $details['password'] = $this->secret('Password');
  67. $details['confirm_password'] = $this->secret('Confirm password');
  68.  
  69. while (! $this->isValidPassword($details['password'], $details['confirm_password'])) {
  70. if (! $this->isRequiredLength($details['password'])) {
  71. $this->error('Password must be more that six characters');
  72. }
  73.  
  74. if (! $this->isMatch($details['password'], $details['confirm_password'])) {
  75. $this->error('Password and Confirm password do not match');
  76. }
  77.  
  78. $details['password'] = $this->secret('Password');
  79. $details['confirm_password'] = $this->secret('Confirm password');
  80. }
  81.  
  82. return $details;
  83. }
  84.  
  85. /**
  86. * Display created admin.
  87. *
  88. * @param array $admin
  89. * @return void
  90. */
  91. private function display(User $admin) : void
  92. {
  93. $headers = ['Name', 'Email', 'Super admin'];
  94.  
  95. $fields = [
  96. 'Name' => $admin->name,
  97. 'email' => $admin->email,
  98. 'admin' => $admin->isSuperAdmin()
  99. ];
  100.  
  101. $this->info('Super admin created');
  102. $this->table($headers, [$fields]);
  103. }
  104.  
  105. /**
  106. * Check if password is vailid
  107. *
  108. * @param string $password
  109. * @param string $confirmPassword
  110. * @return boolean
  111. */
  112. private function isValidPassword(string $password, string $confirmPassword) : bool
  113. {
  114. return $this->isRequiredLength($password) &&
  115. $this->isMatch($password, $confirmPassword);
  116. }
  117.  
  118. /**
  119. * Check if password and confirm password matches.
  120. *
  121. * @param string $password
  122. * @param string $confirmPassword
  123. * @return bool
  124. */
  125. private function isMatch(string $password, string $confirmPassword) : bool
  126. {
  127. return $password === $confirmPassword;
  128. }
  129.  
  130. /**
  131. * Checks if password is longer than six characters.
  132. *
  133. * @param string $password
  134. * @return bool
  135. */
  136. private function isRequiredLength(string $password) : bool
  137. {
  138. return strlen($password) > 6;
  139. }
  140. }
Add Comment
Please, Sign In to add comment