Passed
Pull Request — master (#24)
by Christopher
06:57
created

ImportFileMigrations::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

101
                $migrationTypes->isMigrationTypeValid(/** @scrutinizer ignore-type */ array_get($step, 'type'))) {
Loading history...
102 1
                $this->elasticsearchMigrationService->addMigrationStep(
103 1
                    $migration,
104 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

104
                    /** @scrutinizer ignore-type */ array_get($step, 'type'),
Loading history...
105 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

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