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

Oauth2ListClientsAction::run()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 41
ccs 0
cts 33
cp 0
rs 8.8497
cc 6
nc 10
nop 0
crap 42
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\client;
4
5
use League\OAuth2\Server\Grant\GrantTypeInterface;
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\models\Oauth2ClientInterface;
10
use rhertogh\Yii2Oauth2Server\Oauth2Module;
11
use Yii;
12
use yii\base\Action;
13
use yii\base\InvalidArgumentException;
14
use yii\console\ExitCode;
15
use yii\console\widgets\Table;
16
use yii\helpers\Console;
17
18
/**
19
 * @property Oauth2ClientController $controller
20
 */
21
class Oauth2ListClientsAction extends Action
22
{
23
    public function run()
24
    {
25
        $module = $this->controller->module;
26
27
        $clients = $module->getClientRepository()->getAllClients();
28
29
        if (!$clients) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $clients of type rhertogh\Yii2Oauth2Serve...Oauth2ClientInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
30
            $this->controller->stdout('No clients defined. Run `yii ' . $this->controller->uniqueId . '/create` to define one.' . PHP_EOL);
31
            return ExitCode::OK;
32
        }
33
34
        $rows = [];
35
        foreach ($clients as $client) {
36
            $redirectUris = $client->getRedirectUri();
37
            $rows[] = [
38
                'id' => $client->getPrimaryKey(),
39
                'identifier' => $client->getIdentifier(),
40
                'type' => $client->isConfidential() ? 'Confidential' : 'Public',
41
                'redirect_uris' => $redirectUris
42
                    ? ($redirectUris[0]
43
                        . (count($redirectUris) > 1
0 ignored issues
show
Bug introduced by
It seems like $redirectUris can also be of type string; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

43
                        . (count(/** @scrutinizer ignore-type */ $redirectUris) > 1
Loading history...
44
                            ? ' +' . (count($redirectUris) - 1) . ' more'
45
                            : '')
46
                    )
47
                    : '',
48
                'grant_types' => implode(', ', Oauth2Module::getGrantTypeIdentifiers($client->getGrantTypes())),
49
            ];
50
        }
51
52
        $this->controller->stdout(Table::widget([
53
            'headers' => [
54
                'ID',
55
                'Identifier',
56
                'Type',
57
                'Redirect URIs',
58
                'Grant Types',
59
            ],
60
            'rows' => $rows,
61
        ]));
62
63
        return ExitCode::OK;
64
    }
65
}
66