Completed
Pull Request — develop (#57)
by Sam
01:36
created

ApplyMigrationCommand::handle()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Console;
4
5
use League\Pipeline\Pipeline;
6
use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException;
7
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
8
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage;
9
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage;
10
use Nord\Lumen\Elasticsearch\Pipelines\Stages\DetermineTargetVersionStage;
11
use Nord\Lumen\Elasticsearch\Pipelines\Stages\UpdateIndexAliasStage;
12
13
/**
14
 * Class ApplyMigrationCommand
15
 * @package Nord\Lumen\Elasticsearch\Commands\Migrations
16
 */
17
class ApplyMigrationCommand extends AbstractCommand
18
{
19
20
    /**
21
     * @var string
22
     */
23
    protected $signature = 'elastic:migrations:migrate {config : The path to the index configuration file}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Migrates the specified index to a new index using the newest configuration version';
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function handle()
36
    {
37
        $configurationPath = (string)$this->argument('config');
38
39
        $pipeline = new Pipeline([
40
            new DetermineTargetVersionStage(),
41
            new CheckIndexExistsStage($this->elasticsearchService),
42
            new CreateIndexStage($this->elasticsearchService),
43
            new UpdateIndexAliasStage($this->elasticsearchService),
44
        ]);
45
46
        $payload = new ApplyMigrationPayload($configurationPath);
47
48
        try {
49
            $pipeline->process($payload);
50
51
            $this->output->writeln(sprintf('Migrated %s to %s', $payload->getIndexName(),
52
                $payload->getTargetVersionName()));
53
        } catch (IndexExistsException $e) {
54
            $this->output->writeln('No migration required');
55
        }
56
    }
57
}
58