Passed
Push — master ( cc79c7...17dcf4 )
by Max van der
05:28
created

CreateClientCommand::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 0
crap 12
1
<?php
2
3
4
namespace Mvdstam\Oauth2ServerLaravel\Commands;
5
6
7
use Exception;
8
use Illuminate\Console\Command;
9
use Mvdstam\Oauth2ServerLaravel\Repositories\ClientRepository;
10
use Mvdstam\Oauth2ServerLaravel\Repositories\ScopeRepository;
11
use Ramsey\Uuid\Uuid;
12
use Ramsey\Uuid\UuidInterface;
13
14
class CreateClientCommand extends Command
15
{
16
17
    protected $signature = 'oauth2-server:create-client {--id=} {--secret=} {--name=} {--redirect_uri=} {--scope=*}';
18
19
    protected $description = 'Create new clients for your OAuth2 server';
20
21
    /**
22
     * @var ClientRepository
23
     */
24
    protected $clients;
25
26
    /**
27
     * @var ScopeRepository
28
     */
29
    protected $scopes;
30
31
    /**
32
     * CreateClientCommand constructor.
33
     * @param ClientRepository $clients
34
     * @param ScopeRepository $scopes
35
     */
36 16
    public function __construct(ClientRepository $clients, ScopeRepository $scopes)
37
    {
38 16
        parent::__construct();
39
40 16
        $this->clients = $clients;
41 16
        $this->scopes = $scopes;
42 16
    }
43
44
    public function handle()
45
    {
46
        try {
47
            $this->clients->forceCreate([
48
                'id' => (string) $this->getClientId(),
49
                'secret' => (string) $this->getClientSecret(),
50
                'name' => $this->option('name') ?: $this->ask('Please enter a name'),
51
                'redirect_uri' => $this->option('redirect_uri')
52
            ]);
53
        } catch (Exception $e) {
54
            $this->error("An error occurred: {$e->getMessage()}");
55
            return 1;
56
        }
57
58
        $this->info('Client created succesfully');
59
    }
60
61
    /**
62
     * @return UuidInterface
63
     */
64 View Code Duplication
    protected function getClientId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        if (!($clientId = $this->option('id'))) {
67
            $this->info('Generating a random UUID for client id...');
68
            $clientId = Uuid::uuid1();
69
        } else {
70
            $clientId = Uuid::fromString($clientId);
0 ignored issues
show
Bug introduced by
It seems like $clientId defined by \Ramsey\Uuid\Uuid::fromString($clientId) on line 70 can also be of type array; however, Ramsey\Uuid\Uuid::fromString() 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...
71
        }
72
73
        return $clientId;
74
    }
75
76
    /**
77
     * @return UuidInterface
78
     */
79 View Code Duplication
    protected function getClientSecret()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if (!($clientSecret = $this->option('secret'))) {
82
            $this->info('Generating a random UUID for client secret...');
83
            $clientSecret = Uuid::uuid1();
84
        } else {
85
            $clientSecret = Uuid::fromString($clientSecret);
0 ignored issues
show
Bug introduced by
It seems like $clientSecret defined by \Ramsey\Uuid\Uuid::fromString($clientSecret) on line 85 can also be of type array; however, Ramsey\Uuid\Uuid::fromString() 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...
86
        }
87
88
        return $clientSecret;
89
    }
90
91
}
92