Oauth2GenerateImportMigrationAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 29
c 1
b 0
f 0
dl 0
loc 57
ccs 0
cts 32
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 2
A generatedFilshImport() 0 33 5
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\migrations;
4
5
use rhertogh\Yii2Oauth2Server\controllers\console\migrations\base\Oauth2BaseGenerateMigrationsAction;
6
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController;
7
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\migrations\Oauth2GenerateImportMigrationActionInterface;
8
use rhertogh\Yii2Oauth2Server\migrations\import\Oauth2_FilshYii2Oauth2ServerImportMigration;
9
use Yii;
10
use yii\base\InvalidArgumentException;
11
use yii\base\InvalidConfigException;
12
use yii\console\ExitCode;
13
use yii\db\Connection;
14
15
/**
16
 * @property Oauth2MigrationsController $controller
17
 */
18
class Oauth2GenerateImportMigrationAction extends Oauth2BaseGenerateMigrationsAction implements
19
    Oauth2GenerateImportMigrationActionInterface
20
{
21
    /**
22
     * Generate a migration to import data from another Oauth server.
23
     */
24
    public function run($origin)
25
    {
26
        $imports = [
27
            'filsh' => [$this, 'generatedFilshImport'],
28
        ];
29
30
        if (!array_key_exists($origin, $imports)) {
31
            throw new InvalidArgumentException('Unknown import origin "' . $origin . '", available origins: '
32
                . implode(', ', array_keys($imports)) . '.');
33
        }
34
35
        return call_user_func($imports[$origin]);
36
    }
37
38
    /**
39
     * Generate a migration to import data from https://github.com/filsh/yii2-oauth2-server
40
     * @return int
41
     */
42
    protected function generatedFilshImport()
43
    {
44
        $controller = $this->controller;
45
46
        if (!$controller->confirm('Generate migration for "filsh/yii2-oauth2-server" data import?', true)) {
47
            return ExitCode::OK;
48
        }
49
50
        $db = Yii::$app->get($controller->db);
51
        if (!($db instanceof Connection)) {
52
            throw new InvalidConfigException(get_class($controller)
53
                . '::$db must reference an Yii::$app component that is a ' . Connection::class);
54
        }
55
56
        $defaultClientTable = '{{%oauth_clients}}';
57
        if (!$db->getTableSchema($defaultClientTable)) {
58
            $defaultClientTable = null;
59
        }
60
        $clientTable = $this->controller->prompt('Original clients table name?', [
61
            'required' => true,
62
            'default' => $defaultClientTable,
63
            'validator' => function ($input, &$error) use ($db) {
64
                if (!$db->getTableSchema($input)) {
65
                    $error = 'Table "' . $input . '" not found.';
66
                    return false;
67
                }
68
                return true;
69
            },
70
        ]);
71
72
        return $this->generateMigrations([
73
            Oauth2_FilshYii2Oauth2ServerImportMigration::class => [
74
                'clientTable' => $clientTable,
75
            ],
76
        ]);
77
    }
78
}
79