GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 39ef0e...48eca6 )
by Thorsten
14:15
created

PMF_Instance_Elasticsearch::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %
Metric Value
dl 20
loc 20
rs 9.4286
cc 1
eloc 14
nc 1
nop 1
1
<?php
2
3
use Elasticsearch\Client;
4
5
/**
6
 * The phpMyFAQ instances basic Elasticsearch class.
7
 *
8
 * PHP Version 5.5
9
 *
10
 * This Source Code Form is subject to the terms of the Mozilla Public License,
11
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
12
 * obtain one at http://mozilla.org/MPL/2.0/
13
 *
14
 * @category  phpMyFAQ
15
 * @author    Thorsten Rinne <[email protected]>
16
 * @copyright 2015-2016 phpMyFAQ Team
17
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18
 * @link      http://www.phpmyfaq.de
19
 * @since     2015-12-25
20
 */
21
if (!defined('IS_VALID_PHPMYFAQ')) {
22
    exit();
23
}
24
25
/**
26
 * PMF_Instance_Elasticsearch.
27
 *
28
 * @category  phpMyFAQ
29
 * @author    Thorsten Rinne <[email protected]>
30
 * @copyright 2015-2016 phpMyFAQ Team
31
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
32
 * @link      http://www.phpmyfaq.de
33
 * @since     2015-12-25
34
 */
35
class PMF_Instance_Elasticsearch
36
{
37
    /** @var PMF_Configuration */
38
    protected $config;
39
40
    /** @var Client */
41
    protected $client;
42
43
    /**
44
     * Elasticsearch mapping
45
     *
46
     * @var array
47
     */
48
    private $mappings = [
49
        'faqs' => [
50
            '_source' => [
51
                'enabled' => true
52
            ],
53
            'properties' => [
54
                'id' => [
55
                    'type' => 'integer'
56
                ],
57
                'question' => [
58
                    'type' => 'string',
59
                    'analyzer' => 'autocomplete',
60
                    'search_analyzer' => 'standard'
61
                ],
62
                'answer' => [
63
                    'type' => 'string',
64
                    'analyzer' => 'autocomplete',
65
                    'search_analyzer' => 'standard'
66
                ],
67
                'keywords' => [
68
                    'type' => 'string',
69
                    'analyzer' => 'autocomplete',
70
                    'search_analyzer' => 'standard'
71
                ],
72
                'categories' => [
73
                    'type' => 'string',
74
                    'analyzer' => 'autocomplete',
75
                    'search_analyzer' => 'standard'
76
                ]
77
            ]
78
        ]
79
    ];
80
    /**
81
     * Constructor.
82
     *
83
     * @param PMF_Configuration $config
84
     */
85
    public function __construct(PMF_Configuration $config)
86
    {
87
        $this->client = $config->getElasticsearch();
88
        $this->config = $config->getElasticsearchConfig();
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->getElasticsearchConfig() of type array is incompatible with the declared type object<PMF_Configuration> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
    }
90
91
    /**
92
     * Creates the Elasticsearch index.
93
     *
94
     * @return array
95
     */
96
    public function createIndex()
97
    {
98
        $this->client->indices()->create($this->getParams());
99
        return $this->putMapping();;
100
    }
101
102
    /**
103
     * Deletes the Elasticsearch index.
104
     *
105
     * @return array
106
     */
107
    public function dropIndex()
108
    {
109
        return $this->client->indices()->delete(['index' => $this->config['index']]);
110
    }
111
112
    /**
113
     * Puts phpMyFAQ Elasticsearch mapping into index.
114
     *
115
     * @return bool
116
     */
117
    public function putMapping()
118
    {
119
        $response = $this->getMapping();
120
121
        if (0 === count($response[$this->config['index']]['mappings'])) {
122
123
            $params = [
124
                'index' => $this->config['index'],
125
                'type' => $this->config['type'],
126
                'body' => $this->mappings
127
            ];
128
129
            $response = $this->client->indices()->putMapping($params);
130
131
            if (isset($response['acknowledged']) && true === $response['acknowledged']) {
132
                return true;
133
            }
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * Returns the current mapping.
141
     *
142
     * @return array
143
     */
144
    public function getMapping()
145
    {
146
        return $this->client->indices()->getMapping();
147
    }
148
149
    /**
150
     * Indexing of a FAQ
151
     *
152
     * @param array $faq
153
     *
154
     * @return array
155
     */
156 View Code Duplication
    public function index(Array $faq)
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...
157
    {
158
        $params = [
159
            'index' => $this->config['index'],
160
            'type' => $this->config['type'],
161
            'id' => $faq['solution_id'],
162
            'body' => [
163
                'id' => $faq['id'],
164
                'lang' => $faq['lang'],
165
                'question' => $faq['question'],
166
                'answer' => $faq['answer'],
167
                'keywords' => $faq['keywords'],
168
                'category_id' => $faq['category_id']
169
            ]
170
        ];
171
172
        return $this->client->index($params);
173
    }
174
175
    /**
176
     * Bulk indexing of all FAQs
177
     *
178
     * @param array $faqs
179
     *
180
     * @return array
181
     */
182
    public function bulkIndex(Array $faqs)
183
    {
184
        $params = ['body' => []];
185
        $responses = [];
186
        $i = 1;
187
188
        foreach ($faqs as $faq) {
189
            $params['body'][] = [
190
                'index' => [
191
                    '_index' => $this->config['index'],
192
                    '_type' => $this->config['type'],
193
                    '_id' => $faq['solution_id'],
194
                ]
195
            ];
196
197
            $params['body'][] = [
198
                'id' => $faq['id'],
199
                'lang' => $faq['lang'],
200
                'question' => $faq['title'],
201
                'answer' => $faq['content'],
202
                'keywords' => $faq['keywords'],
203
                'category_id' => $faq['category_id']
204
            ];
205
206
            if ($i % 1000 == 0) {
207
                $responses = $this->client->bulk($params);
208
                $params = ['body' => []];
209
                unset($responses);
210
            }
211
212
            $i++;
213
        }
214
215
        // Send the last batch if it exists
216
        if (!empty($params['body'])) {
217
            $responses = $this->client->bulk($params);
218
        }
219
220
        return $responses;
221
    }
222
223
    /**
224
     * Updates a FAQ document
225
     *
226
     * @param array $faq
227
     * @return array
228
     */
229 View Code Duplication
    public function update(Array $faq)
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...
230
    {
231
        $params = [
232
            'index' => $this->config['index'],
233
            'type' => $this->config['type'],
234
            'id' => $faq['solution_id'],
235
            'body' => [
236
                'doc' => [
237
                    'id' => $faq['id'],
238
                    'lang' => $faq['lang'],
239
                    'question' => $faq['question'],
240
                    'answer' => $faq['answer'],
241
                    'keywords' => $faq['keywords'],
242
                    'category_id' => $faq['category_id']
243
                ]
244
            ]
245
        ];
246
247
        return $this->client->update($params);
248
    }
249
250
    /**
251
     * Deletes a FAQ document
252
     *
253
     * @param integer $solutionId
254
     * @return array
255
     */
256
    public function delete($solutionId)
257
    {
258
        $params = [
259
            'index' => $this->config['index'],
260
            'type' => $this->config['type'],
261
            'id' => $solutionId
262
        ];
263
264
        return $this->client->delete($params);
265
    }
266
267
    /**
268
     * Returns the basic phpMyFAQ index structure as raw array.
269
     *
270
     * @return array
271
     */
272
    private function getParams()
273
    {
274
        return [
275
            'index' => $this->config['index'],
276
            'body' => [
277
                'settings' => [
278
                    'number_of_shards' => PMF_ELASTICSEARCH_NUMBER_SHARDS,
279
                    'number_of_replicas' => PMF_ELASTICSEARCH_NUMBER_REPLICAS,
280
                    'analysis' => [
281
                        'filter' => [
282
                            'autocomplete_filter' => [
283
                                'type' =>'edge_ngram',
284
                                'min_gram' => 1,
285
                                'max_gram' => 20
286
                            ]
287
                        ],
288
                        'analyzer' => [
289
                            'autocomplete' => [
290
                                'type' => 'custom',
291
                                'tokenizer' => 'standard',
292
                                'filter' => [
293
                                    'lowercase',
294
                                    'autocomplete_filter'
295
                                ]
296
                            ]
297
                        ]
298
                    ]
299
                ]
300
            ]
301
        ];
302
    }
303
}
304