Passed
Push — master ( f52d5c...0b49e9 )
by Rutger
03:13
created

Oauth2CreateClientAction::validateScope()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.6111
cc 5
nc 3
nop 2
crap 30
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\client;
4
5
use League\OAuth2\Server\Grant\GrantTypeInterface;
6
use rhertogh\Yii2Oauth2Server\controllers\console\client\base\Oauth2BaseEditClientAction;
7
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController;
8
use rhertogh\Yii2Oauth2Server\helpers\DiHelper;
9
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
11
use rhertogh\Yii2Oauth2Server\Oauth2Module;
12
use Yii;
13
use yii\base\Action;
14
use yii\base\InvalidArgumentException;
15
use yii\console\ExitCode;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Console;
18
19
/**
20
 * @property Oauth2ClientController $controller
21
 */
22
class Oauth2CreateClientAction extends Oauth2BaseEditClientAction
23
{
24
    public function run()
25
    {
26
        $controller = $this->controller;
27
        $module = $controller->module;
28
29
        /** @var class-string<Oauth2ClientInterface> $clientClass */
30
        $clientClass = DiHelper::getValidatedClassName(Oauth2ClientInterface::class);
31
        /** @var Oauth2ClientInterface $client */
32
        $client = new $clientClass;
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
33
34
        if (!empty($controller->sample)) {
35
            $sample = strtolower($controller->sample);
36
37
            if ($sample == 'postman') {
38
                $postmanIdentifier = 'postman-sample-client';
39
                $defaultIdentifier = $postmanIdentifier;
40
                $postmanIdentifierCount = 1;
41
                while ($clientClass::findByIdentifier($defaultIdentifier)) {
42
                    $defaultIdentifier = $postmanIdentifier . '-' . ++$postmanIdentifierCount;
43
                }
44
                $client->setIdentifier($defaultIdentifier);
45
                $client->setName('Postman Sample Client');
46
                $client->setRedirectUri(['https://oauth.pstmn.io/v1/callback']);
47
48
                $defaultGrantTypes = 0;
49
                foreach ($module->getAuthorizationServer()->getEnabledGrantTypes() as $grantType) {
50
                    $defaultGrantTypes |= Oauth2Module::getGrantTypeId($grantType->getIdentifier());
51
                }
52
                $client->setGrantTypes($defaultGrantTypes);
53
54
                if ($module->enableOpenIdConnect) {
55
                    $defaultScopes = implode(' ', Oauth2OidcScopeCollectionInterface::OPENID_CONNECT_DEFAULT_SCOPES);
56
                }
57
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
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