Oauth2GeneratePatAction::run()   B
last analyzed

Complexity

Conditions 11
Paths 64

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 49
c 3
b 0
f 0
dl 0
loc 77
ccs 0
cts 56
cp 0
rs 7.3166
cc 11
nc 64
nop 0
crap 132

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 rhertogh\Yii2Oauth2Server\controllers\console\Oauth2PersonalAccessTokenController;
6
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\PersonalAccessToken\Oauth2GeneratePatActionInterface;
7
use rhertogh\Yii2Oauth2Server\Oauth2Module;
8
use yii\base\Action;
9
use yii\console\ExitCode;
10
11
/**
12
 * @property Oauth2PersonalAccessTokenController $controller
13
 */
14
class Oauth2GeneratePatAction extends Action implements Oauth2GeneratePatActionInterface
15
{
16
    /**
17
     * Generate an Oauth2 "Personal Access Token".
18
     */
19
    public function run()
20
    {
21
        $module = $this->controller->module;
22
23
        $clientIdentifier = $this->controller->client;
24
        $clientSecret = $this->controller->clientSecret;
25
        $userId = $this->controller->user;
26
        $scope = $this->controller->scope;
27
28
        if (empty($clientIdentifier)) {
29
            $clientIdentifier = $this->controller->prompt('Client Identifier?', [
30
                'required' => true,
31
                'default' => $this->controller->defaultClientIdentifier ?? null,
32
                'validator' => function ($input, &$error) {
33
                    $client = $this->getClient($input);
34
                    if ($client) {
35
                        if (!$client->validateGrantType(Oauth2Module::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN)) {
36
                            $error = 'The "Personal Access Token" grant type is not enabled for client "'
37
                                . $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' => $this->controller->defaultClientSecret ?? null,
55
            ]);
56
        }
57
58
        if ($clientSecret === 'true') {
59
            $clientSecret = true;
60
        }
61
62
        if (empty($userId)) {
63
            $userId = $this->controller->prompt('User Identifier?', [
64
                'required' => true,
65
                'default' => $this->controller->defaultUserId ?? null,
66
                'validator' => function ($input, &$error) {
67
                    $user = $this->getUser($input);
68
                    if (!$user) {
69
                        $error = 'No user with identifier "' . $input . '" found.';
70
                        return false;
71
                    }
72
                    return true;
73
                },
74
            ]);
75
        }
76
77
        if (empty($scope)) {
78
            $scope = $this->controller->prompt('Scope?', [
79
                'required' => false,
80
                'default' => $this->controller->defaultScope ?? null,
81
            ]);
82
        }
83
84
        $result = $module->generatePersonalAccessToken(
85
            $clientIdentifier,
86
            $userId,
87
            $scope,
88
            $clientSecret,
89
        );
90
91
        foreach ($result as $key => $value) {
92
            $this->controller->stdout($key . ': ' . $value . PHP_EOL);
93
        }
94
95
        return ExitCode::OK;
96
    }
97
98
    protected function getClient($identifier)
99
    {
100
        return $this->controller->module->getClientRepository()->getClientEntity($identifier);
101
    }
102
103
    protected function getUser($identifier)
104
    {
105
        return $this->controller->module->getUserRepository()->getUserEntityByIdentifier($identifier);
106
    }
107
}
108