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

Oauth2GenerateMigrationsAction::generateNewMigrationClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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\base\Oauth2BaseMigration;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * @property Oauth2MigrationsController $controller
12
 */
13
class Oauth2GenerateMigrationsAction extends Oauth2BaseGenerateMigrationsAction
14
{
15 7
    public function run()
16
    {
17 7
        $sourcePath = $this->getMigrationsSourcePath();
18 7
        $sourceNamespace = $this->getMigrationsSourceNamespace();
19 7
        $sourceMigrationClasses = $this->getSourceMigrationClasses($sourcePath, $sourceNamespace);
20
21 7
        return $this->generateMigrations($sourceMigrationClasses);
22
    }
23
24
    /**
25
     * @param string $migrationPath
26
     * @param string $namespace
27
     * @return class-string<Oauth2BaseMigration>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Oauth2BaseMigration>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Oauth2BaseMigration>[].
Loading history...
28
     */
29 8
    protected function getSourceMigrationClasses($migrationPath, $namespace)
30
    {
31 8
        if (!file_exists($migrationPath)) {
32 1
            throw new InvalidConfigException('Source migration path "' . $migrationPath . '" does not exist.');
33
        }
34
35 7
        $migrations = [];
36 7
        $handle = opendir($migrationPath);
37 7
        while (($file = readdir($handle)) !== false) {
38 7
            if ($file === '.' || $file === '..') {
39 7
                continue;
40
            }
41 7
            $path = $migrationPath . DIRECTORY_SEPARATOR . $file;
42 7
            if (preg_match('/^(Oauth2_\d{5}_.+Migration)\.php$/is', $file, $matches) && is_file($path)) {
43 7
                $migrations[] = $namespace . '\\' . $matches[1];
44
            }
45
        }
46 7
        closedir($handle);
47
48 7
        sort($migrations);
49
50 7
        return $migrations;
51
    }
52
53 7
    protected function getMigrationsSourcePath()
54
    {
55 7
        $reflector = new \ReflectionClass($this->controller->module);
56 7
        $moduleFile = $reflector->getFileName();
57
58 7
        return dirname($moduleFile) . DIRECTORY_SEPARATOR . 'migrations';
59
    }
60
61 7
    protected function getMigrationsSourceNamespace()
62
    {
63 7
        $reflector = new \ReflectionClass($this->controller->module);
64 7
        return $reflector->getNamespaceName() . '\\migrations';
65
    }
66
}
67