Passed
Push — master ( 207765...57309d )
by Rutger
03:12
created

Oauth2GenerateImportMigrationAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 33
cp 0
rs 10

2 Methods

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