Oauth2SetClientSecretAction::run()   B
last analyzed

Complexity

Conditions 10
Paths 30

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 59
ccs 0
cts 36
cp 0
rs 7.6666
cc 10
nc 30
nop 0
crap 110

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\client;
4
5
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController;
6
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2SetClientSecretActionInterface;
7
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
8
use Yii;
9
use yii\base\Action;
10
use yii\base\InvalidArgumentException;
11
use yii\base\InvalidCallException;
12
use yii\base\InvalidConfigException;
13
use yii\console\ExitCode;
14
use yii\helpers\Console;
15
use yii\helpers\StringHelper;
16
17
/**
18
 * @property Oauth2ClientController $controller
19
 */
20
class Oauth2SetClientSecretAction extends Action implements Oauth2SetClientSecretActionInterface
21
{
22
    /**
23
     * Update the "client secret" of an Oauth2 Client.
24
     *
25
     * @throws InvalidConfigException
26
     */
27
    public function run()
28
    {
29
        $module = $this->controller->module;
30
31
        $identifier = $this->controller->identifier;
32
        $secret = $this->controller->secret;
33
        $oldSecretValidUntilInput = $this->controller->oldSecretValidUntil;
34
35
        if (empty($identifier)) {
36
            throw new InvalidCallException('The `identifier` option must be specified.');
37
        }
38
39
        /** @var Oauth2ClientInterface $client */
40
        $client = $module->getClientRepository()->findModelByIdentifier($identifier);
41
        if (!$client) {
0 ignored issues
show
introduced by
$client is of type rhertogh\Yii2Oauth2Serve...s\Oauth2ClientInterface, thus it always evaluated to true.
Loading history...
42
            throw new InvalidCallException('No client with identifier "' . $identifier . '" found.');
43
        }
44
45
        if (empty($secret)) {
46
            $secret = $this->controller->prompt('Client Secret?', [
47
                'required' => true,
48
                'validator' => [$client, 'validateNewSecret'],
49
            ]);
50
        }
51
52
        $oldSecretValidUntil = null;
53
        if ($oldSecretValidUntilInput) {
54
            if (StringHelper::startsWith($oldSecretValidUntilInput, 'P')) {
55
                try {
56
                    $oldSecretValidUntil = new \DateInterval($oldSecretValidUntilInput);
57
                } catch (\Exception $e) {
58
                    $oldSecretValidUntil = false;
59
                }
60
            } else {
61
                try {
62
                    $oldSecretValidUntil = new \DateTimeImmutable($oldSecretValidUntilInput);
63
                } catch (\Exception $e) {
64
                    $oldSecretValidUntil = false;
65
                }
66
            }
67
            if (empty($oldSecretValidUntil)) {
68
                throw new InvalidArgumentException('Unable to parse "' . $oldSecretValidUntilInput
69
                    . '" as a DateTime or DateInterval.');
70
            }
71
        }
72
73
        $client->setSecret($secret, $module->getCryptographer(), $oldSecretValidUntil);
0 ignored issues
show
Bug introduced by
It seems like $oldSecretValidUntil can also be of type false; however, parameter $oldSecretValidUntil of rhertogh\Yii2Oauth2Serve...tInterface::setSecret() does only seem to accept DateInterval|DateTimeImmutable|null, 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

73
        $client->setSecret($secret, $module->getCryptographer(), /** @scrutinizer ignore-type */ $oldSecretValidUntil);
Loading history...
74
        $client->persist();
75
76
        if ($oldSecretValidUntil) {
77
            $oldSecretValidUntilInfo = ' The previous secret will be valid until '
78
                . Yii::$app->formatter->asDatetime($client->getOldSecretValidUntil(), 'long');
79
        } else {
80
            $oldSecretValidUntilInfo = ' Any previous secret is cleared.';
81
        }
82
        $this->controller->stdout('Successfully updated client secret.' . $oldSecretValidUntilInfo
83
            . PHP_EOL, Console::FG_GREEN);
84
85
        return ExitCode::OK;
86
    }
87
}
88