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

Installation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 12.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 15
loc 116
ccs 0
cts 71
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setOptions() 15 15 3
B start() 0 60 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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