Passed
Push — master ( b74962...219f1c )
by Ivan
04:39
created

SetCredentials::handle()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 16
nop 1
crap 5
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);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type array; however, Illuminate\Support\Facades\Hash::make() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        }
41
42 9
        $updater->set('STAGEFRONT_LOGIN', $username);
0 ignored issues
show
Bug introduced by
It seems like $username defined by $this->argument('usernam...?: $this->askUsername() on line 34 can also be of type array; however, CodeZero\DotEnvUpdater\DotEnvUpdater::set() does only seem to accept string|integer|boolean, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43 9
        $updater->set('STAGEFRONT_PASSWORD', $password);
0 ignored issues
show
Bug introduced by
It seems like $password defined by $this->argument('passwor...?: $this->askPassword() on line 35 can also be of type array; however, CodeZero\DotEnvUpdater\DotEnvUpdater::set() does only seem to accept string|integer|boolean, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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