|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\StageFront\Commands; |
| 4 | + |
| 5 | +use CodeZero\DotEnvUpdater\DotEnvUpdater; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\Hash; |
| 8 | + |
| 9 | +class SetCredentials extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'stagefront:credentials {username?} {password?} {--encrypt : Encrypt the password}'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Set a username and password to login to the staging site.'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Execute the console command. |
| 27 | + * |
| 28 | + * @param \CodeZero\DotEnvUpdater\DotEnvUpdater $updater |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function handle(DotEnvUpdater $updater) |
| 33 | + { |
| 34 | + $username = $this->argument('username') ?: $this->askUsername(); |
| 35 | + $password = $this->argument('password') ?: $this->askPassword(); |
| 36 | + $encrypt = $this->option('encrypt') ?: $this->askForEncryption(); |
| 37 | + |
| 38 | + if ($encrypt) { |
| 39 | + $password = Hash::make($password); |
| 40 | + } |
| 41 | + |
| 42 | + $updater->set('STAGEFRONT_LOGIN', $username); |
| 43 | + $updater->set('STAGEFRONT_PASSWORD', $password); |
| 44 | + $updater->set('STAGEFRONT_ENCRYPT', $encrypt); |
| 45 | + |
| 46 | + $this->info("StageFront credentials were written to [.env]."); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Ask for a username. |
| 51 | + * |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + protected function askUsername() |
| 55 | + { |
| 56 | + $username = trim($this->ask('Choose a username:', 'stagefront')); |
| 57 | + |
| 58 | + if ($username) { |
| 59 | + return $username; |
| 60 | + } |
| 61 | + |
| 62 | + $this->error('Username can not be empty. Try again...'); |
| 63 | + |
| 64 | + return $this->askUsername(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Ask for a password and confirmation. |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + protected function askPassword() |
| 73 | + { |
| 74 | + $password = $this->secret('Choose a password:'); |
| 75 | + |
| 76 | + if (trim($password)) { |
| 77 | + return $this->askPasswordConfirmation($password); |
| 78 | + } |
| 79 | + |
| 80 | + $this->error('Password can not be empty. Try again...'); |
| 81 | + |
| 82 | + return $this->askPassword(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Ask for a password confirmation. |
| 87 | + * |
| 88 | + * @param string $password |
| 89 | + * |
| 90 | + * @return string |
| 91 | + */ |
| 92 | + protected function askPasswordConfirmation($password) |
| 93 | + { |
| 94 | + $passwordConfirmation = $this->secret('Retype password:'); |
| 95 | + |
| 96 | + if ($password === $passwordConfirmation) { |
| 97 | + return $password; |
| 98 | + } |
| 99 | + |
| 100 | + $this->error('Password did not match. Try again...'); |
| 101 | + |
| 102 | + return $this->askPassword(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Ask if the password should be encrypted. |
| 107 | + * |
| 108 | + * @return bool |
| 109 | + */ |
| 110 | + protected function askForEncryption() |
| 111 | + { |
| 112 | + if ($this->argument('password')) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return $this->confirm('Encrypt password?'); |
| 117 | + } |
| 118 | +} |
0 commit comments