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 ( 0fedc1...47b4c9 )
by Thorsten
15:08
created

PMF_Instance_Elasticsearch::getMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 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 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' => 'standard'
60
                ],
61
                'answer' => [
62
                    'type' => 'string',
63
                    'analyzer' => 'standard'
64
                ],
65
                'keywords' => [
66
                    'type' => 'string',
67
                    'analyzer' => 'standard'
68
                ],
69
                'categories' => [
70
                    'type' => 'string',
71
                    'analyzer' => 'standard'
72
                ]
73
            ]
74
        ]
75
    ];
76
    /**
77
     * Constructor.
78
     *
79
     * @param PMF_Configuration $config
80
     */
81
    public function __construct(PMF_Configuration $config)
82
    {
83
        $this->client = $config->getElasticsearch();
84
        $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...
85
    }
86
87
    /**
88
     * Creates the Elasticsearch index.
89
     *
90
     * @return array
91
     */
92
    public function createIndex()
93
    {
94
        $this->client->indices()->create($this->getParams());
95
        return $this->putMapping();;
96
    }
97
98
    /**
99
     * Deletes the Elasticsearch index.
100
     *
101
     * @return array
102
     */
103
    public function dropIndex()
104
    {
105
        return $this->client->indices()->delete(['index' => $this->config['index']]);
106
    }
107
108
    /**
109
     * Puts phpMyFAQ Elasticsearch mapping into index.
110
     *
111
     * @return bool
112
     */
113
    public function putMapping()
114
    {
115
        $response = $this->getMapping();
116
117
        if (0 === count($response[$this->config['index']]['mappings'])) {
118
119
            $params = [
120
                'index' => $this->config['index'],
121
                'type' => $this->config['type'],
122
                'body' => $this->mappings
123
            ];
124
125
            $response = $this->client->indices()->putMapping($params);
126
127
            if (isset($response['acknowledged']) && true === $response['acknowledged']) {
128
                return true;
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * Returns the current mapping.
137
     *
138
     * @return array
139
     */
140
    public function getMapping()
141
    {
142
        return $this->client->indices()->getMapping();
143
    }
144
145
    public function index(Array $faq)
0 ignored issues
show
Unused Code introduced by
The parameter $faq is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
    {
147
148
    }
149
150
    /**
151
     * Bulk indexing of all FAQs
152
     *
153
     * @param array $faqs
154
     *
155
     * @return array
156
     */
157
    public function bulkIndex(Array $faqs)
158
    {
159
        $params = ['body' => []];
160
        $responses = [];
161
        $i = 1;
162
163
        foreach ($faqs as $faq) {
164
            $params['body'][] = [
165
                'index' => [
166
                    '_index' => $this->config['index'],
167
                    '_type' => $this->config['type'],
168
                    '_id' => $i
169
                ]
170
            ];
171
172
            $params['body'][] = [
173
                'id' => $faq['id'],
174
                'question' => $faq['title'],
175
                'answer' => $faq['content'],
176
                'keywords' => $faq['keywords'],
177
                'categories' => $faq['category_id']
178
            ];
179
180
            if ($i % 1000 == 0) {
181
                $responses = $this->client->bulk($params);
182
                $params = ['body' => []];
183
                unset($responses);
184
            }
185
186
            $i++;
187
        }
188
189
        // Send the last batch if it exists
190
        if (!empty($params['body'])) {
191
            $responses = $this->client->bulk($params);
192
        }
193
194
        return $responses;
195
    }
196
197
    public function update(Array $faq)
0 ignored issues
show
Unused Code introduced by
The parameter $faq is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
198
    {
199
200
    }
201
202
    public function delete($faqId)
0 ignored issues
show
Unused Code introduced by
The parameter $faqId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    {
204
205
    }
206
207
    /**
208
     * Returns the basic phpMyFAQ index structure as raw array.
209
     *
210
     * @return array
211
     */
212
    private function getParams()
213
    {
214
        return [
215
            'index' => $this->config['index'],
216
            'body' => [
217
                'settings' => [
218
                    'number_of_shards' => 2,
219
                    'number_of_replicas' => 0
220
                ]
221
            ]
222
        ];
223
    }
224
}
225