Oauth2CreateClientAction::run()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 49
ccs 0
cts 31
cp 0
rs 8.0555
cc 9
nc 7
nop 0
crap 90
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\client;
4
5
use rhertogh\Yii2Oauth2Server\controllers\console\client\base\Oauth2BaseEditClientAction;
6
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController;
7
use rhertogh\Yii2Oauth2Server\helpers\DiHelper;
8
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface;
9
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2CreateClientActionInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
11
use rhertogh\Yii2Oauth2Server\Oauth2Module;
12
use yii\base\InvalidArgumentException;
13
use yii\console\ExitCode;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Console;
16
17
/**
18
 * @property Oauth2ClientController $controller
19
 */
20
class Oauth2CreateClientAction extends Oauth2BaseEditClientAction implements Oauth2CreateClientActionInterface
21
{
22
    /**
23
     * Create a new Oauth2 Client.
24
     */
25
    public function run()
26
    {
27
        $controller = $this->controller;
28
        $module = $controller->module;
29
30
        /** @var class-string<Oauth2ClientInterface> $clientClass */
31
        $clientClass = DiHelper::getValidatedClassName(Oauth2ClientInterface::class);
32
        /** @var Oauth2ClientInterface $client */
33
        $client = new $clientClass();
34
35
        if (!empty($controller->sample)) {
36
            $sample = strtolower($controller->sample);
37
38
            if ($sample == 'postman') {
39
                $postmanIdentifier = 'postman-sample-client';
40
                $defaultIdentifier = $postmanIdentifier;
41
                $postmanIdentifierCount = 1;
42
                while ($clientClass::findByIdentifier($defaultIdentifier)) {
43
                    $defaultIdentifier = $postmanIdentifier . '-' . ++$postmanIdentifierCount;
44
                }
45
                $client->setIdentifier($defaultIdentifier);
46
                $client->setName('Postman Sample Client');
47
                $client->setRedirectUri(['https://oauth.pstmn.io/v1/callback']);
48
49
                $defaultGrantTypes = 0;
50
                foreach ($module->getAuthorizationServer()->getEnabledGrantTypes() as $grantType) {
51
                    $defaultGrantTypes |= Oauth2Module::getGrantTypeId($grantType->getIdentifier());
52
                }
53
                $client->setGrantTypes($defaultGrantTypes);
54
55
                if ($module->enableOpenIdConnect) {
56
                    $defaultScopes = implode(' ', Oauth2OidcScopeCollectionInterface::OPENID_CONNECT_DEFAULT_SCOPES);
57
                }
58
            } else {
59
                throw new InvalidArgumentException('Unknown client sample: "' . $sample . '"');
60
            }
61
        }
62
63
        $this->editClient($client, $defaultScopes ?? '');
64
        $clientScopes = $client->getClientScopes()->with('scope')->all();
0 ignored issues
show
Unused Code introduced by
The call to yii\db\ActiveQueryInterface::with() has too many arguments starting with 'scope'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $clientScopes = $client->getClientScopes()->/** @scrutinizer ignore-call */ with('scope')->all();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
        $scopes = implode(' ', ArrayHelper::getColumn($clientScopes, 'scope.identifier'));
66
67
        if ($controller->interactive || $controller->verbose) {
68
            $controller->stdout('Successfully created new client with id "' . $client->getPrimaryKey()
69
                . '", identifier "' . $client->getIdentifier()
70
                . '"' . ($scopes ? (' and scopes "' . $scopes . '"') : '') . '.' . PHP_EOL, Console::FG_GREEN);
71
        }
72
73
        return ExitCode::OK;
74
    }
75
}
76