Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

Installation::start()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 52
cp 0
rs 8.5616
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Elasticsearch\Migration\Delta;
13
14
use Balloon\App\Elasticsearch\Exception;
15
use Balloon\Migration\Delta\DeltaInterface;
16
use Elasticsearch\Client;
17
use InvalidArgumentException;
18
use Psr\Log\LoggerInterface;
19
20
class Installation implements DeltaInterface
21
{
22
    /**
23
     * Elasticsearch.
24
     *
25
     * @var Client
26
     */
27
    protected $client;
28
29
    /**
30
     * Logger.
31
     *
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
36
    /**
37
     * Index configuration.
38
     *
39
     * @var string
40
     */
41
    protected $index_configuration = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'index.json';
42
43
    /**
44
     * Construct.
45
     */
46
    public function __construct(Client $client, LoggerInterface $logger, array $config = [])
47
    {
48
        $this->client = $client;
49
        $this->logger = $logger;
50
        $this->setOptions($config);
51
    }
52
53
    /**
54
     * Set options.
55
     */
56 View Code Duplication
    public function setOptions(array $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        foreach ($config as $key => $value) {
59
            switch ($key) {
60
                case 'index_configuration':
61
                    $this->{$key} = (string) $value;
62
63
                break;
64
                default:
65
                    throw new InvalidArgumentException('invalid option '.$key.' given');
66
            }
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function start(): bool
76
    {
77
        $this->logger->info('create elasticsearch indices blobs and nodes', [
78
            'category' => get_class($this),
79
        ]);
80
81
        $this->logger->debug('read index configuration from ['.$this->index_configuration.']', [
82
            'category' => get_class($this),
83
        ]);
84
85
        if (!is_readable($this->index_configuration)) {
86
            throw new Exception\IndexConfigurationNotFound('index configuration '.$this->index_configuration.' not found');
87
        }
88
89
        $index = json_decode(file_get_contents($this->index_configuration));
90
        if (json_last_error() !== JSON_ERROR_NONE) {
91
            throw new Exception\InvalidIndexConfiguration('invalid elasticsearch index configuration json given');
92
        }
93
94
        foreach ($index as $name => $settings) {
95
            $this->logger->info('create elasticsearch index ['.$name.']', [
96
                'category' => get_class($this),
97
                'settings' => $settings,
98
            ]);
99
100
            try {
101
                $this->client->indices()->create([
102
                    'index' => $name,
103
                    'body' => $settings,
104
                ]);
105
            } catch (\Exception $e) {
106
                $this->logger->error('can not create index, try to update existing index', [
107
                    'category' => get_class($this),
108
                    'exception' => $e,
109
                ]);
110
111
                $this->client->indices()->putMapping([
112
                    'index' => $name,
113
                    'type' => '_doc',
114
                    'body' => $settings->mappings,
115
                ]);
116
            }
117
        }
118
119
        $this->client->ingest()->putPipeline([
120
            'id' => 'attachments',
121
            'body' => [
122
                'processors' => [
123
                    [
124
                        'attachment' => [
125
                            'field' => 'content',
126
                            'indexed_chars' => -1,
127
                        ],
128
                    ],
129
                ],
130
            ],
131
        ]);
132
133
        return true;
134
    }
135
}
136