Oauth2GeneratePatAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
eloc 52
c 3
b 0
f 0
dl 0
loc 92
ccs 0
cts 60
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 77 11
A getUser() 0 3 1
A getClient() 0 3 1
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