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

CreateUserCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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