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

Oauth2RotateEncryptionKeysAction::run()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 15
cp 0
rs 9.7998
cc 4
nc 6
nop 0
crap 20
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\encryption;
4
5
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2EncryptionController;
6
use yii\base\Action;
7
use yii\base\InvalidArgumentException;
8
use yii\console\ExitCode;
9
use yii\console\widgets\Table;
10
use yii\helpers\Console;
11
12
/**
13
 * @property Oauth2EncryptionController $controller
14
 */
15
class Oauth2RotateEncryptionKeysAction extends Action
16
{
17
    public function run()
18
    {
19
        $module = $this->controller->module;
20
        $newKeyName = $this->controller->keyName;
21
22
        if (empty($newKeyName)) {
23
            $newKeyName = $module->getEncryptor()->getDefaultKeyName();
24
        }
25
26
        if (!$module->getEncryptor()->hasKey($newKeyName)) {
27
            throw new InvalidArgumentException('No key with name "' . $newKeyName . '" available.');
28
        }
29
30
        if ($this->controller->confirm('Rotate all encryption keys to key "' . $newKeyName . '"?')) {
31
            $result = $module->rotateStorageEncryptionKeys($newKeyName);
32
33
            $this->controller->stdout("Updated models:" . PHP_EOL);
34
            $this->controller->stdout(Table::widget([
35
                'headers' => ['Model', 'Num Updated'],
36
                'rows' => array_map(fn($model) => [$model, $result[$model]], array_keys($result)),
37
            ]));
38
            $this->controller->stdout('Successfully rotated encryption keys.'  . PHP_EOL, Console::FG_GREEN);
39
        }
40
41
        return ExitCode::OK;
42
    }
43
}
44