Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

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.
Passed
Pull Request — master (#329)
by Sebastian
03:28
created

DataHandler::processCmdmap_postProcess()   B

Complexity

Conditions 10
Paths 19

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 19
nop 5
dl 0
loc 39
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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
namespace Kitodo\Dlf\Hooks;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
use Kitodo\Dlf\Common\Document;
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\Solr;
17
18
/**
19
 * Hooks and helper for \TYPO3\CMS\Core\DataHandling\DataHandler
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class DataHandler {
27
    /**
28
     * Field post-processing hook for the process_datamap() method.
29
     *
30
     * @access public
31
     *
32
     * @param string $status: 'new' or 'update'
33
     * @param string $table: The destination table
34
     * @param integer $id: The uid of the record
35
     * @param array &$fieldArray: Array of field values
36
     * @param \TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
37
     *
38
     * @return void
39
     */
40
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, $pObj) {
1 ignored issue
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, /** @scrutinizer ignore-unused */ $pObj) {

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

Loading history...
41
        if ($status == 'new') {
42
            switch ($table) {
43
                // Field post-processing for table "tx_dlf_documents".
44
                case 'tx_dlf_documents':
45
                    // Set sorting field if empty.
46
                    if (empty($fieldArray['title_sorting'])
47
                        && !empty($fieldArray['title'])) {
48
                        $fieldArray['title_sorting'] = $fieldArray['title'];
49
                    }
50
                    break;
51
                // Field post-processing for table "tx_dlf_metadata".
52
                case 'tx_dlf_metadata':
53
                    // Store field in index if it should appear in lists.
54
                    if (!empty($fieldArray['is_listed'])) {
55
                        $fieldArray['index_stored'] = 1;
56
                    }
57
                    // Index field in index if it should be used for auto-completion.
58
                    if (!empty($fieldArray['index_autocomplete'])) {
59
                        $fieldArray['index_indexed'] = 1;
60
                    }
61
                // Field post-processing for tables "tx_dlf_metadata", "tx_dlf_collections", "tx_dlf_libraries" and "tx_dlf_structures".
62
                case 'tx_dlf_collections':
63
                case 'tx_dlf_libraries':
64
                case 'tx_dlf_structures':
65
                    // Set label as index name if empty.
66
                    if (empty($fieldArray['index_name'])
67
                        && !empty($fieldArray['label'])) {
68
                        $fieldArray['index_name'] = $fieldArray['label'];
69
                    }
70
                    // Set index name as label if empty.
71
                    if (empty($fieldArray['label'])
72
                        && !empty($fieldArray['index_name'])) {
73
                        $fieldArray['label'] = $fieldArray['index_name'];
74
                    }
75
                    // Ensure that index names don't get mixed up with sorting values.
76
                    if (substr($fieldArray['index_name'], -8) == '_sorting') {
77
                        $fieldArray['index_name'] .= '0';
78
                    }
79
                    break;
80
                // Field post-processing for table "tx_dlf_solrcores".
81
                case 'tx_dlf_solrcores':
82
                    // Get number of existing cores.
83
                    $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
84
                        '*',
85
                        'tx_dlf_solrcores',
86
                        '',
87
                        '',
88
                        '',
89
                        ''
90
                    );
91
                    // Get first unused core number.
92
                    $coreNumber = Solr::solrGetCoreNumber($GLOBALS['TYPO3_DB']->sql_num_rows($result));
93
                    // Get Solr credentials.
94
                    $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']);
95
                    $solrInfo = Solr::getSolrConnectionInfo();
96
                    // Prepend username and password to hostname.
97
                    if ($solrInfo['username']
98
                        && $solrInfo['password']) {
99
                        $host = $solrInfo['username'].':'.$solrInfo['password'].'@'.$solrInfo['host'];
100
                    } else {
101
                        $host = $solrInfo['host'];
102
                    }
103
                    $context = stream_context_create([
104
                        'http' => [
105
                            'method' => 'GET',
106
                            'user_agent' => ($conf['useragent'] ? $conf['useragent'] : ini_get('user_agent'))
107
                        ]
108
                    ]);
109
                    // Build request for adding new Solr core.
110
                    // @see http://wiki.apache.org/solr/CoreAdmin
111
                    $url = $solrInfo['scheme'].'://'.$host.':'.$solrInfo['port'].'/'.$solrInfo['path'].'/admin/cores?wt=xml&action=CREATE&name=dlfCore'.$coreNumber.'&instanceDir=dlfCore'.$coreNumber.'&dataDir=data&configSet=dlf';
112
                    $response = @simplexml_load_string(file_get_contents($url, FALSE, $context));
113
                    // Process response.
114
                    if ($response) {
115
                        $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]');
116
                        if ($status
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type SimpleXMLElement[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
                            && $status[0] == 0) {
0 ignored issues
show
introduced by
The condition $status[0] == 0 is always false.
Loading history...
118
                            $fieldArray['index_name'] = 'dlfCore'.$coreNumber;
119
                            return;
120
                        }
121
                    }
122
                    Helper::devLog('Could not create new Apache Solr core "dlfCore'.$coreNumber.'"', DEVLOG_SEVERITY_ERROR);
123
                    // Solr core could not be created, thus unset field array.
124
                    $fieldArray = [];
125
                    break;
126
            }
127
        } elseif ($status == 'update') {
128
            switch ($table) {
129
                // Field post-processing for table "tx_dlf_metadata".
130
                case 'tx_dlf_metadata':
131
                    // Store field in index if it should appear in lists.
132
                    if (!empty($fieldArray['is_listed'])) {
133
                        $fieldArray['index_stored'] = 1;
134
                    }
135
                    if (isset($fieldArray['index_stored'])
136
                        && $fieldArray['index_stored'] == 0
137
                        && !isset($fieldArray['is_listed'])) {
138
                        // Get current configuration.
139
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
140
                            $table.'.is_listed AS is_listed',
141
                            $table,
142
                            $table.'.uid='.intval($id)
143
                                .Helper::whereClause($table),
144
                            '',
145
                            '',
146
                            '1'
147
                        );
148
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
149
                            // Reset storing to current.
150
                            list ($fieldArray['index_stored']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
151
                        }
152
                    }
153
                    // Index field in index if it should be used for auto-completion.
154
                    if (!empty($fieldArray['index_autocomplete'])) {
155
                        $fieldArray['index_indexed'] = 1;
156
                    }
157
                    if (isset($fieldArray['index_indexed'])
158
                        && $fieldArray['index_indexed'] == 0
159
                        && !isset($fieldArray['index_autocomplete'])) {
160
                        // Get current configuration.
161
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
162
                            $table.'.index_autocomplete AS index_autocomplete',
163
                            $table,
164
                            $table.'.uid='.intval($id)
165
                                .Helper::whereClause($table),
166
                            '',
167
                            '',
168
                            '1'
169
                        );
170
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
171
                            // Reset indexing to current.
172
                            list ($fieldArray['index_indexed']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
173
                        }
174
                    }
175
                // Field post-processing for tables "tx_dlf_metadata" and "tx_dlf_structures".
176
                case 'tx_dlf_structures':
177
                    // The index name should not be changed in production.
178
                    if (isset($fieldArray['index_name'])) {
179
                        if (count($fieldArray) < 2) {
180
                            // Unset the whole field array.
181
                            $fieldArray = [];
182
                        } else {
183
                            // Get current index name.
184
                            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
185
                                $table.'.index_name AS index_name',
186
                                $table,
187
                                $table.'.uid='.intval($id)
188
                                    .Helper::whereClause($table),
189
                                '',
190
                                '',
191
                                '1'
192
                            );
193
                            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
194
                                // Reset index name to current.
195
                                list ($fieldArray['index_name']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
196
                            }
197
                        }
198
                        Helper::devLog('Prevented change of index_name for UID '.$id.' in table "'.$table.'"', DEVLOG_SEVERITY_NOTICE);
199
                    }
200
                    break;
201
            }
202
        }
203
    }
204
205
    /**
206
     * After database operations hook for the process_datamap() method.
207
     *
208
     * @access public
209
     *
210
     * @param string $status: 'new' or 'update'
211
     * @param string $table: The destination table
212
     * @param integer $id: The uid of the record
213
     * @param array &$fieldArray: Array of field values
214
     * @param \TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
215
     *
216
     * @return void
217
     */
218
    public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, $pObj) {
1 ignored issue
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, /** @scrutinizer ignore-unused */ $pObj) {

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

Loading history...
219
        if ($status == 'update') {
220
            switch ($table) {
221
                // After database operations for table "tx_dlf_documents".
222
                case 'tx_dlf_documents':
223
                    // Delete/reindex document in Solr if "hidden" status or collections have changed.
224
                    if (isset($fieldArray['hidden'])
225
                        || isset($fieldArray['collections'])) {
226
                        // Get Solr core.
227
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
228
                            'tx_dlf_solrcores.uid,tx_dlf_documents.hidden',
229
                            'tx_dlf_solrcores,tx_dlf_documents',
230
                            'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore'
231
                                .' AND tx_dlf_documents.uid='.intval($id)
232
                                .Helper::whereClause('tx_dlf_solrcores'),
233
                            '',
234
                            '',
235
                            '1'
236
                        );
237
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
238
                            list ($core, $hidden) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
239
                            if ($hidden) {
240
                                // Establish Solr connection.
241
                                if ($solr = Solr::getInstance($core)) {
242
                                    // Delete Solr document.
243
                                    $updateQuery = $solr->service->createUpdate();
0 ignored issues
show
Bug Best Practice introduced by
The property $service is declared protected in Kitodo\Dlf\Common\Solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
244
                                    $updateQuery->addDeleteQuery('uid:'.$id);
245
                                    $updateQuery->addCommit();
246
                                    $solr->service->update($updateQuery);
247
                                }
248
                            } else {
249
                                // Reindex document.
250
                                $doc = Document::getInstance($id);
251
                                if ($doc->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
252
                                    $doc->save($doc->pid, $core);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
253
                                } else {
254
                                    Helper::devLog('Failed to re-index document with UID '.$id, DEVLOG_SEVERITY_ERROR);
255
                                }
256
                            }
257
                        }
258
                    }
259
                    break;
260
            }
261
        }
262
    }
263
264
    /**
265
     * Post-processing hook for the process_cmdmap() method.
266
     *
267
     * @access public
268
     *
269
     * @param string $command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete'
270
     * @param string $table: The destination table
271
     * @param integer $id: The uid of the record
272
     * @param mixed $value: The value for the command
273
     * @param \TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
274
     *
275
     * @return void
276
     */
277
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj) {
2 ignored issues
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

277
    public function processCmdmap_postProcess($command, $table, $id, $value, /** @scrutinizer ignore-unused */ $pObj) {

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

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

277
    public function processCmdmap_postProcess($command, $table, $id, /** @scrutinizer ignore-unused */ $value, $pObj) {

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

Loading history...
278
        if (in_array($command, ['move', 'delete', 'undelete'])
279
            && $table == 'tx_dlf_documents') {
280
            // Get Solr core.
281
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
282
                'tx_dlf_solrcores.uid',
283
                'tx_dlf_solrcores,tx_dlf_documents',
284
                'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore'
285
                    .' AND tx_dlf_documents.uid='.intval($id)
286
                    .Helper::whereClause('tx_dlf_solrcores'),
287
                '',
288
                '',
289
                '1'
290
            );
291
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
292
                list ($core) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
293
                switch ($command) {
294
                    case 'move':
295
                    case 'delete':
296
                        // Establish Solr connection.
297
                        if ($solr = Solr::getInstance($core)) {
298
                            // Delete Solr document.
299
                            $updateQuery = $solr->service->createUpdate();
0 ignored issues
show
Bug Best Practice introduced by
The property $service is declared protected in Kitodo\Dlf\Common\Solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
300
                            $updateQuery->addDeleteQuery('uid:'.$id);
301
                            $updateQuery->addCommit();
302
                            $solr->service->update($updateQuery);
303
                            if ($command == 'delete') {
304
                                break;
305
                            }
306
                        }
307
                    case 'undelete':
308
                        // Reindex document.
309
                        $doc = Document::getInstance($id);
310
                        if ($doc->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
311
                            $doc->save($doc->pid, $core);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
312
                        } else {
313
                            Helper::devLog('Failed to re-index document with UID '.$id, DEVLOG_SEVERITY_ERROR);
314
                        }
315
                        break;
316
                }
317
            }
318
        }
319
    }
320
}
321