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

Oauth2RotateEncryptionKeysAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 15
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 25 4
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