Passed
Branch master (9b4352)
by Rutger
13:03
created

Oauth2SetClientSecretAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 39
c 1
b 0
f 0
dl 0
loc 64
ccs 0
cts 36
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 59 10
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\interfaces\models\Oauth2ClientScopeInterface;
11
use rhertogh\Yii2Oauth2Server\Oauth2Module;
12
use Yii;
13
use yii\base\Action;
14
use yii\base\InvalidArgumentException;
15
use yii\base\InvalidCallException;
16
use yii\console\ExitCode;
17
use yii\helpers\Console;
18
use yii\helpers\StringHelper;
19
20
/**
21
 * @property Oauth2ClientController $controller
22
 */
23
class Oauth2SetClientSecretAction extends Action
24
{
25
    /**
26
     * @throws \yii\base\InvalidConfigException
27
     */
28
    public function run()
29
    {
30
        $module = $this->controller->module;
31
32
        $identifier = $this->controller->identifier;
33
        $secret = $this->controller->secret;
34
        $oldSecretValidUntilInput = $this->controller->oldSecretValidUntil;
35
36
        if (empty($identifier)) {
37
            throw new InvalidCallException('The `identifier` option must be specified.');
38
        }
39
40
        /** @var Oauth2ClientInterface $client */
41
        $client = $module->getClientRepository()->findModelByIdentifier($identifier);
42
        if (empty($client)) {
43
            throw new InvalidCallException('No client with identifier "' . $identifier . '" found.');
44
        }
45
46
        if (empty($secret)) {
47
            $secret = $this->controller->prompt('Client Secret?', [
48
                'required' => true,
49
                'validator' => [$client, 'validateNewSecret'],
50
            ]);
51
        }
52
53
        $oldSecretValidUntil = null;
54
        if ($oldSecretValidUntilInput) {
55
            if (StringHelper::startsWith($oldSecretValidUntilInput, 'P')) {
56
                try {
57
                    $oldSecretValidUntil = new \DateInterval($oldSecretValidUntilInput);
58
                } catch (\Exception $e) {
59
                    $oldSecretValidUntil = false;
60
                }
61
            } else {
62
                try {
63
                    $oldSecretValidUntil = new \DateTimeImmutable($oldSecretValidUntilInput);
64
                } catch (\Exception $e) {
65
                    $oldSecretValidUntil = false;
66
                }
67
            }
68
            if (empty($oldSecretValidUntil)) {
69
                throw new InvalidArgumentException('Unable to parse "' . $oldSecretValidUntilInput
70
                    . '" as a DateTime or DateInterval.');
71
            }
72
        }
73
74
        $client->setSecret($secret, $module->getEncryptor(), $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

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