Completed
Pull Request — develop (#39)
by Sam
02:08
created

ApplyMigrationCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 2
A __construct() 0 5 1
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Console;
4
5
use Illuminate\Console\Command;
6
use League\Pipeline\Pipeline;
7
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
8
use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException;
9
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
10
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage;
11
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage;
12
use Nord\Lumen\Elasticsearch\Pipelines\Stages\DetermineTargetVersionStage;
13
use Nord\Lumen\Elasticsearch\Pipelines\Stages\UpdateIndexAliasStage;
14
15
/**
16
 * Class ApplyMigrationCommand
17
 * @package Nord\Lumen\Elasticsearch\Commands\Migrations
18
 */
19
class ApplyMigrationCommand extends Command
20
{
21
22
    /**
23
     * @var string
24
     */
25
    protected $signature = 'search:migrations:migrate {config : The path to the index configuration file}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Migrates the specified index to a new index using the newest configuration version';
33
34
    /**
35
     * @var ElasticsearchServiceContract
36
     */
37
    protected $elasticsearchService;
38
39
    /**
40
     * AbstractMigrationCommand constructor.
41
     *
42
     * @param ElasticsearchServiceContract $elasticsearchService
43
     */
44
    public function __construct(ElasticsearchServiceContract $elasticsearchService)
45
    {
46
        parent::__construct();
47
48
        $this->elasticsearchService = $elasticsearchService;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function handle()
55
    {
56
        $configurationPath = $this->argument('config');
57
58
        $pipeline = new Pipeline([
59
            new DetermineTargetVersionStage(),
60
            new CheckIndexExistsStage($this->elasticsearchService),
61
            new CreateIndexStage($this->elasticsearchService),
62
            new UpdateIndexAliasStage($this->elasticsearchService),
63
        ]);
64
65
        $payload = new ApplyMigrationPayload($configurationPath);
0 ignored issues
show
Bug introduced by
It seems like $configurationPath can also be of type array; however, parameter $configurationPath of Nord\Lumen\Elasticsearch...nPayload::__construct() 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

65
        $payload = new ApplyMigrationPayload(/** @scrutinizer ignore-type */ $configurationPath);
Loading history...
66
67
        try {
68
            $pipeline->process($payload);
69
70
            $this->output->writeln(sprintf('Migrated %s to %s', $payload->getIndexName(),
71
                $payload->getTargetVersionName()));
72
        } catch (IndexExistsException $e) {
73
            $this->output->writeln('No migration required');
74
        }
75
    }
76
}
77