|
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
|
9 |
|
public function handle(DotEnvUpdater $updater) |
|
33
|
|
|
{ |
|
34
|
9 |
|
$username = $this->argument('username') ?: $this->askUsername(); |
|
35
|
9 |
|
$password = $this->argument('password') ?: $this->askPassword(); |
|
36
|
9 |
|
$encrypt = $this->option('encrypt') ?: $this->askForEncryption(); |
|
37
|
|
|
|
|
38
|
9 |
|
if ($encrypt) { |
|
39
|
3 |
|
$password = Hash::make($password); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
$updater->set('STAGEFRONT_LOGIN', $username); |
|
|
|
|
|
|
43
|
9 |
|
$updater->set('STAGEFRONT_PASSWORD', $password); |
|
|
|
|
|
|
44
|
9 |
|
$updater->set('STAGEFRONT_ENCRYPT', $encrypt); |
|
45
|
|
|
|
|
46
|
9 |
|
$this->info("StageFront credentials were written to [.env]."); |
|
47
|
9 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Ask for a username. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
6 |
|
protected function askUsername() |
|
55
|
|
|
{ |
|
56
|
6 |
|
$username = trim($this->ask('Choose a username:', 'stagefront')); |
|
57
|
|
|
|
|
58
|
6 |
|
if ($username) { |
|
59
|
6 |
|
return $username; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
$this->error('Username can not be empty. Try again...'); |
|
63
|
|
|
|
|
64
|
1 |
|
return $this->askUsername(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Ask for a password and confirmation. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
7 |
|
protected function askPassword() |
|
73
|
|
|
{ |
|
74
|
7 |
|
$password = $this->secret('Choose a password:'); |
|
75
|
|
|
|
|
76
|
7 |
|
if (trim($password)) { |
|
77
|
7 |
|
return $this->askPasswordConfirmation($password); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
$this->error('Password can not be empty. Try again...'); |
|
81
|
|
|
|
|
82
|
1 |
|
return $this->askPassword(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Ask for a password confirmation. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $password |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
7 |
|
protected function askPasswordConfirmation($password) |
|
93
|
|
|
{ |
|
94
|
7 |
|
$passwordConfirmation = $this->secret('Retype password:'); |
|
95
|
|
|
|
|
96
|
7 |
|
if ($password === $passwordConfirmation) { |
|
97
|
7 |
|
return $password; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
$this->error('Password did not match. Try again...'); |
|
101
|
|
|
|
|
102
|
1 |
|
return $this->askPassword(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Ask if the password should be encrypted. |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
7 |
|
protected function askForEncryption() |
|
111
|
|
|
{ |
|
112
|
7 |
|
if ($this->argument('password')) { |
|
113
|
1 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
return $this->confirm('Encrypt password?'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.