CreateUserCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 46
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 15 4
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