Completed
Push — master ( bafc8d...4dabfd )
by Phan
08:25
created

ChangePasswordCommand::validatePasswords()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 10
cc 4
nc 3
nop 2
crap 20
1
<?php
2
3
namespace App\Console\Commands\Admin;
4
5
use App\Console\Commands\Traits\AskForPassword;
6
use App\Models\User;
7
use Illuminate\Console\Command;
8
use Illuminate\Contracts\Hashing\Hasher as Hash;
9
10
class ChangePasswordCommand extends Command
11
{
12
    use AskForPassword;
13
14
    protected $name = 'koel:admin:change-password';
15
    protected $description = "Change the default admin's password";
16
17
    private $hash;
18
19 132
    public function __construct(Hash $hash)
20
    {
21 132
        parent::__construct();
22 132
        $this->hash = $hash;
23 132
    }
24
25
    public function handle(): void
26
    {
27
        /** @var User|null $user */
28
        $user = User::where('is_admin', true)->first();
29
30
        if (!$user) {
31
            $this->error('An admin account cannot be found. Have you set up Koel yet?');
32
33
            return;
34
        }
35
36
        $this->comment("Changing the default admin's password (ID: {$user->id}, email: {$user->email})");
37
38
        $user->password = $this->hash->make($this->askForPassword());
39
        $user->save();
40
41
        $this->comment('Alrighty, your new password has been saved. Enjoy! 👌');
42
    }
43
}
44