Passed
Push — master ( 7f1549...5746c2 )
by Max van der
06:50
created

CreateUserCommand::handle()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 0
crap 20
1
<?php
2
3
4
namespace Mvdstam\Oauth2ServerLaravel\Commands;
5
6
7
use Exception;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Hash;
10
use Mvdstam\Oauth2ServerLaravel\Repositories\UserRepository;
11
use Ramsey\Uuid\Uuid;
12
13
class CreateUserCommand extends Command
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $signature = 'oauth2-server:create-user {--username=} {--password=}';
20
21
    /**
22
     * @var string
23
     */
24
    protected $description = 'Create new users for your OAuth2 server';
25
26
    /**
27
     * @var UserRepository
28
     */
29
    protected $users;
30
31
    /**
32
     * CreateUserCommand constructor.
33
     * @param UserRepository $users
34
     */
35 16
    public function __construct(UserRepository $users)
36
    {
37 16
        parent::__construct();
38
        
39 16
        $this->users = $users;
40 16
    }
41
42
    public function handle()
43
    {
44
        try {
45
            $this->users->forceCreate([
46
                'id' => (string) Uuid::uuid1(),
47
                'username' => $this->option('username') ?: $this->ask('Please enter desired username'),
48
                'password' => Hash::make($this->option('password') ?: $this->ask('Please enter desired password'))
49
            ]);
50
        } catch (Exception $e) {
51
            $this->error("An error occurred: {$e->getMessage()}");
52
            return 1;
53
        }
54
55
        $this->info('User created successfully');
56
    }
57
58
}
59