CheckIndexExistsStage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 11 2
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Pipelines\Stages;
4
5
use League\Pipeline\StageInterface;
6
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
7
use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException;
8
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
9
10
/**
11
 * Class CheckIndexExistsStage
12
 * @package Nord\Lumen\Elasticsearch\Pipelines\Stages
13
 */
14
class CheckIndexExistsStage implements StageInterface
15
{
16
17
    /**
18
     * @var ElasticsearchServiceContract
19
     */
20
    private $elasticsearchService;
21
22
    /**
23
     * CheckIndexExistsStage constructor.
24
     *
25
     * @param ElasticsearchServiceContract $elasticsearchService
26
     */
27
    public function __construct(ElasticsearchServiceContract $elasticsearchService)
28
    {
29
        $this->elasticsearchService = $elasticsearchService;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     *
35
     * @throws IndexExistsException
36
     */
37
    public function __invoke($payload)
38
    {
39
        /** @var ApplyMigrationPayload $payload */
40
        $index    = $payload->getPrefixedTargetVersionName();
41
        $response = $this->elasticsearchService->indices()->exists(['index' => $index]);
42
43
        if ($response) {
44
            throw new IndexExistsException($index);
45
        }
46
47
        return $payload;
48
    }
49
}
50