Passed
Push — master ( 60854d...29b3a1 )
by Rutger
03:10
created

Oauth2GeneratePatAction::run()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 72
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 72
ccs 0
cts 39
cp 0
rs 7.3115
cc 10
nc 32
nop 0
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\PersonalAccessToken;
4
5
use League\OAuth2\Server\Grant\GrantTypeInterface;
6
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2DebugController;
7
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2PersonalAccessTokenController;
8
use rhertogh\Yii2Oauth2Server\helpers\DiHelper;
9
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
10
use rhertogh\Yii2Oauth2Server\Oauth2Module;
11
use yii\base\Action;
12
use yii\console\ExitCode;
13
use yii\console\widgets\Table;
14
15
/**
16
 * @property Oauth2PersonalAccessTokenController $controller
17
 */
18
class Oauth2GeneratePatAction extends Action
19
{
20
    public function run()
21
    {
22
        $module = $this->controller->module;
23
24
        $clientIdentifier = $this->controller->client;
25
        $clientSecret = $this->controller->clientSecret;
26
        $userId = $this->controller->user;
27
        $scope = $this->controller->scope;
28
29
        if (empty($clientIdentifier)) {
30
            $clientIdentifier = $this->controller->prompt('Client Identifier?', [
31
                'required' => true,
32
                'default' => $defaultClientIdentifier ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultClientIdentifier seems to never exist and therefore isset should always be false.
Loading history...
33
                'validator' => function ($input, &$error) {
34
                    $client = $this->getClient($input);
35
                    if ($client) {
0 ignored issues
show
introduced by
$client is of type rhertogh\Yii2Oauth2Serve...s\Oauth2ClientInterface, thus it always evaluated to true.
Loading history...
36
                        if (!$client->validateGrantType(Oauth2Module::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN)) {
37
                            $error = 'The "Personal Access Token" grant type is not enabled for client "' . $input . '".';
38
                            return false;
39
                        }
40
                    } else {
41
                        $error = 'No client with identifier "' . $input . '" found.';
42
                        return false;
43
                    }
44
                    return true;
45
                },
46
            ]);
47
        }
48
49
        $client = $this->getClient($clientIdentifier);
50
51
        if (empty($clientSecret) && $client->isConfidential()) {
52
            $clientSecret = $this->controller->prompt('Client Secret?', [
53
                'required' => true,
54
                'default' => $defaultClientSecret ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultClientSecret seems to never exist and therefore isset should always be false.
Loading history...
55
            ]);
56
        }
57
58
        if (empty($userId)) {
59
            $userId = $this->controller->prompt('User Identifier?', [
60
                'required' => true,
61
                'default' => $defaultUserId ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultUserId seems to never exist and therefore isset should always be false.
Loading history...
62
                'validator' => function ($input, &$error) {
63
                    $user = $this->getUser($input);
64
                    if (!$user) {
65
                        $error = 'No user with identifier "' . $input . '" found.';
66
                        return false;
67
                    }
68
                    return true;
69
                },
70
            ]);
71
        }
72
73
        if (empty($scope)) {
74
            $scope = $this->controller->prompt('Scope?', [
75
                'required' => false,
76
                'default' => $defaultScope ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultScope seems to never exist and therefore isset should always be false.
Loading history...
77
            ]);
78
        }
79
80
        $result = $module->generatePersonalAccessToken(
81
            $clientIdentifier,
82
            $userId,
83
            $scope,
84
            $clientSecret,
85
        );
86
87
        foreach ($result as $key => $value) {
88
            $this->controller->stdout($key . ': ' . $value . PHP_EOL);
89
        }
90
91
        return ExitCode::OK;
92
    }
93
94
    protected function getClient($identifier)
95
    {
96
        return $this->controller->module->getClientRepository()->getClientEntity($identifier);
97
    }
98
99
    protected function getUser($identifier)
100
    {
101
        return $this->controller->module->getUserRepository()->getUserEntityByIdentifier($identifier);
102
    }
103
}
104