Completed
Pull Request — master (#24)
by Christopher
09:42 queued 06:24
created

ImportFileMigrations::importMigrationSteps()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
namespace Triadev\EsMigration\Console\Commands;
3
4
use Illuminate\Console\Command;
5
use Triadev\EsMigration\Business\Mapper\MigrationTypes;
6
use Triadev\EsMigration\Contract\ElasticsearchMigrationContract;
7
8
class ImportFileMigrations extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'triadev:es:import_file_migrations {migration}';
16
    
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Import file migrations.';
23
    
24
    /** @var ElasticsearchMigrationContract */
25
    private $elasticsearchMigrationService;
26
    
27
    /**
28
     * ImportFileMigrations constructor.
29
     * @param ElasticsearchMigrationContract $elasticsearchMigrationService
30
     */
31 63
    public function __construct(ElasticsearchMigrationContract $elasticsearchMigrationService)
32
    {
33 63
        parent::__construct();
34
        
35 63
        $this->elasticsearchMigrationService = $elasticsearchMigrationService;
36 63
    }
37
    
38
    /**
39
     * Execute the console command.
40
     *
41
     * @throws \Exception
42
     */
43 3
    public function handle()
44
    {
45 3
        $migration = (string)$this->argument('migration');
46
        
47 3
        $filePath = config('triadev-elasticsearch-migration.filePath');
48 3
        if (!$filePath) {
49 1
            throw new \Exception("No migration file path was defined.");
50
        }
51
        
52 2
        $migrationSteps = $this->getMigrationSteps($migration, $filePath);
53 1
        if (!empty($migrationSteps)) {
54 1
            $this->elasticsearchMigrationService->createMigration($migration);
55
            
56 1
            $this->importMigrationSteps(
57 1
                $migration,
58 1
                $migrationSteps
59
            );
60
        }
61 1
    }
62
    
63
    /**
64
     * @param string $migration
65
     * @param string $filePath
66
     * @return array
67
     * @throws \Exception
68
     */
69 2
    private function getMigrationSteps(string $migration, string $filePath) : array
70
    {
71 2
        $filePath = $filePath . DIRECTORY_SEPARATOR . $migration;
72 2
        if (!is_dir($filePath)) {
73 1
            throw new \Exception("The migration directory does not exist.");
74
        }
75
        
76 1
        $files = [];
77
    
78 1
        foreach (scandir($filePath) as $key => $value)
79
        {
80 1
            if (!in_array($value, ['.', '..'])) {
81 1
                $files[] = $filePath . DIRECTORY_SEPARATOR . $value;
82
            }
83
        }
84
        
85 1
        return $files;
86
    }
87
    
88
    /**
89
     * @param string $migration
90
     * @param array $migrationSteps
91
     */
92 1
    private function importMigrationSteps(string $migration, array $migrationSteps)
93
    {
94 1
        $migrationTypes = new MigrationTypes();
95
        
96 1
        foreach ($migrationSteps as $migrationStep) {
97 1
            $step = require $migrationStep;
98
            
99 1
            if (array_has($step, ['type', 'params']) &&
100 1
                $migrationTypes->isMigrationTypeValid(array_get($step, 'type'))) {
0 ignored issues
show
Bug introduced by
It seems like array_get($step, 'type') can also be of type null; however, parameter $type of Triadev\EsMigration\Busi...:isMigrationTypeValid() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                $migrationTypes->isMigrationTypeValid(/** @scrutinizer ignore-type */ array_get($step, 'type'))) {
Loading history...
101 1
                $this->elasticsearchMigrationService->addMigrationStep(
102 1
                    $migration,
103 1
                    array_get($step, 'type'),
0 ignored issues
show
Bug introduced by
It seems like array_get($step, 'type') can also be of type null; however, parameter $type of Triadev\EsMigration\Cont...act::addMigrationStep() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                    /** @scrutinizer ignore-type */ array_get($step, 'type'),
Loading history...
104 1
                    array_get($step, 'params'),
0 ignored issues
show
Bug introduced by
It seems like array_get($step, 'params') can also be of type null; however, parameter $params of Triadev\EsMigration\Cont...act::addMigrationStep() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
                    /** @scrutinizer ignore-type */ array_get($step, 'params'),
Loading history...
105 1
                    array_get($step, 'priority', 1),
106 1
                    array_get($step, 'stopOnFailure', true)
107
                );
108
            }
109
        }
110 1
    }
111
}
112