tx_dlf_tcemain   F
last analyzed

Complexity

Total Complexity 65

Size/Duplication

Total Lines 422
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 422
rs 3.3333
c 0
b 0
f 0
wmc 65

3 Methods

Rating   Name   Duplication   Size   Complexity  
C processDatamap_afterDatabaseOperations() 0 64 9
F processDatamap_postProcessFieldArray() 0 244 45
C processCmdmap_postProcess() 0 59 11

How to fix   Complexity   

Complex Class

Complex classes like tx_dlf_tcemain often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use tx_dlf_tcemain, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Hooks and helper for the '\TYPO3\CMS\Core\DataHandling\DataHandler' library.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_tcemain {
21
22
    /**
23
     * Field post-processing hook for the process_datamap() method.
24
     *
25
     * @access	public
26
     *
27
     * @param	string		$status: 'new' or 'update'
28
     * @param	string		$table: The destination table
29
     * @param	integer		$id: The uid of the record
30
     * @param	array		&$fieldArray: Array of field values
31
     * @param	\TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\DataHandling\DataHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     *
33
     * @return	void
34
     */
35
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, $pObj) {
36
37
        if ($status == 'new') {
38
39
            switch ($table) {
40
41
                // Field post-processing for table "tx_dlf_documents".
42
                case 'tx_dlf_documents':
43
44
                    // Set sorting field if empty.
45
                    if (empty($fieldArray['title_sorting']) && !empty($fieldArray['title'])) {
46
47
                        $fieldArray['title_sorting'] = $fieldArray['title'];
48
49
                    }
50
51
                    break;
52
53
                    // Field post-processing for table "tx_dlf_metadata".
54
                case 'tx_dlf_metadata':
55
56
                    // Store field in index if it should appear in lists.
57
                    if (!empty($fieldArray['is_listed'])) {
58
59
                        $fieldArray['index_stored'] = 1;
60
61
                    }
62
63
                    // Index field in index if it should be used for auto-completion.
64
                    if (!empty($fieldArray['index_autocomplete'])) {
65
66
                        $fieldArray['index_indexed'] = 1;
67
68
                    }
69
70
                    // Field post-processing for tables "tx_dlf_metadata", "tx_dlf_collections", "tx_dlf_libraries" and "tx_dlf_structures".
71
                case 'tx_dlf_collections':
72
                case 'tx_dlf_libraries':
73
                case 'tx_dlf_structures':
74
75
                    // Set label as index name if empty.
76
                    if (empty($fieldArray['index_name']) && !empty($fieldArray['label'])) {
77
78
                        $fieldArray['index_name'] = $fieldArray['label'];
79
80
                    }
81
82
                    // Set index name as label if empty.
83
                    if (empty($fieldArray['label']) && !empty($fieldArray['index_name'])) {
84
85
                        $fieldArray['label'] = $fieldArray['index_name'];
86
87
                    }
88
89
                    // Ensure that index names don't get mixed up with sorting values.
90
                    if (substr($fieldArray['index_name'], -8) == '_sorting') {
91
92
                        $fieldArray['index_name'] .= '0';
93
94
                    }
95
96
                    break;
97
98
                    // Field post-processing for table "tx_dlf_solrcores".
99
                case 'tx_dlf_solrcores':
100
101
                    // Get number of existing cores.
102
                    $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
103
                        '*',
104
                        'tx_dlf_solrcores',
105
                        '',
106
                        '',
107
                        '',
108
                        ''
109
                    );
110
111
                    // Get first unused core number.
112
                    $coreNumber = tx_dlf_solr::solrGetCoreNumber($GLOBALS['TYPO3_DB']->sql_num_rows($result));
113
114
                    // Get Solr credentials.
115
                    $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']);
116
117
                    // Prepend username and password to hostname.
118
                    if ($conf['solrUser'] && $conf['solrPass']) {
119
120
                        $host = $conf['solrUser'].':'.$conf['solrPass'].'@'.($conf['solrHost'] ? $conf['solrHost'] : 'localhost');
121
122
                    } else {
123
124
                        $host = ($conf['solrHost'] ? $conf['solrHost'] : 'localhost');
125
126
                    }
127
128
                    // Set port if not set.
129
                    $port = (intval($conf['solrPort']) > 0 ? intval($conf['solrPort']) : 8180);
130
131
                    // Trim path and append trailing slash.
132
                    $path = (trim($conf['solrPath'], '/') ? trim($conf['solrPath'], '/').'/' : '');
133
134
                    $context = stream_context_create(array (
135
                        'http' => array (
136
                            'method' => 'GET',
137
                            'user_agent' => ($conf['useragent'] ? $conf['useragent'] : ini_get('user_agent'))
138
                        )
139
                    ));
140
141
                    // Build request for adding new Solr core.
142
                    // @see http://wiki.apache.org/solr/CoreAdmin
143
                    $url = 'http://'.$host.':'.$port.'/'.$path.'admin/cores?wt=xml&action=CREATE&name=dlfCore'.$coreNumber.'&instanceDir=.&dataDir=dlfCore'.$coreNumber;
144
145
                    $response = @simplexml_load_string(file_get_contents($url, FALSE, $context));
146
147
                    // Process response.
148
                    if ($response) {
149
150
                        $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]');
151
152
                        if ($status && $status[0] == 0) {
153
154
                            $fieldArray['index_name'] = 'dlfCore'.$coreNumber;
155
156
                            return;
157
158
                        }
159
160
                    }
161
162
                    if (TYPO3_DLOG) {
0 ignored issues
show
Bug introduced by
The constant TYPO3_DLOG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
164
                        \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_tcemain->processDatamap_postProcessFieldArray('.$status.', '.$table.', '.$id.', [data], ['.get_class($pObj).'])] Could not create new Apache Solr core "dlfCore'.$coreNumber.'"', $this->extKey, SYSLOG_SEVERITY_ERROR, $fieldArray);
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug Best Practice introduced by
The property extKey does not exist on tx_dlf_tcemain. Did you maybe forget to declare it?
Loading history...
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
165
166
                    }
167
168
                    // Solr core could not be created, thus unset field array.
169
                    $fieldArray = array ();
170
171
                    break;
172
173
            }
174
175
        } elseif ($status == 'update') {
176
177
            switch ($table) {
178
179
                    // Field post-processing for table "tx_dlf_metadata".
180
                case 'tx_dlf_metadata':
181
182
                    // Store field in index if it should appear in lists.
183
                    if (!empty($fieldArray['is_listed'])) {
184
185
                        $fieldArray['index_stored'] = 1;
186
187
                    }
188
189
                    if (isset($fieldArray['index_stored']) && $fieldArray['index_stored'] == 0 && !isset($fieldArray['is_listed'])) {
190
191
                        // Get current configuration.
192
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
193
                            $table.'.is_listed AS is_listed',
194
                            $table,
195
                            $table.'.uid='.intval($id).tx_dlf_helper::whereClause($table),
196
                            '',
197
                            '',
198
                            '1'
199
                        );
200
201
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
202
203
                            // Reset storing to current.
204
                            list ($fieldArray['index_stored']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
205
206
                        }
207
208
                    }
209
210
                    // Index field in index if it should be used for auto-completion.
211
                    if (!empty($fieldArray['index_autocomplete'])) {
212
213
                        $fieldArray['index_indexed'] = 1;
214
215
                    }
216
217
                    if (isset($fieldArray['index_indexed']) && $fieldArray['index_indexed'] == 0 && !isset($fieldArray['index_autocomplete'])) {
218
219
                        // Get current configuration.
220
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
221
                            $table.'.index_autocomplete AS index_autocomplete',
222
                            $table,
223
                            $table.'.uid='.intval($id).tx_dlf_helper::whereClause($table),
224
                            '',
225
                            '',
226
                            '1'
227
                        );
228
229
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
230
231
                            // Reset indexing to current.
232
                            list ($fieldArray['index_indexed']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
233
234
                        }
235
236
                    }
237
238
                    // Field post-processing for tables "tx_dlf_metadata" and "tx_dlf_structures".
239
                case 'tx_dlf_structures':
240
241
                    // The index name should not be changed in production.
242
                    if (isset($fieldArray['index_name'])) {
243
244
                        if (count($fieldArray) < 2) {
245
246
                            // Unset the whole field array.
247
                            $fieldArray = array ();
248
249
                        } else {
250
251
                            // Get current index name.
252
                            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
253
                                $table.'.index_name AS index_name',
254
                                $table,
255
                                $table.'.uid='.intval($id).tx_dlf_helper::whereClause($table),
256
                                '',
257
                                '',
258
                                '1'
259
                            );
260
261
                            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
262
263
                                // Reset index name to current.
264
                                list ($fieldArray['index_name']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
265
266
                            }
267
268
                        }
269
270
                        if (TYPO3_DLOG) {
271
272
                            \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_tcemain->processDatamap_postProcessFieldArray('.$status.', '.$table.', '.$id.', [data], ['.get_class($pObj).'])] Prevented change of "index_name" for UID "'.$id.'" in table "'.$table.'"', $this->extKey, SYSLOG_SEVERITY_NOTICE, $fieldArray);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_NOTICE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
273
274
                        }
275
276
                    }
277
278
                    break;
279
280
            }
281
282
        }
283
284
    }
285
286
    /**
287
     * After database operations hook for the process_datamap() method.
288
     *
289
     * @access	public
290
     *
291
     * @param	string		$status: 'new' or 'update'
292
     * @param	string		$table: The destination table
293
     * @param	integer		$id: The uid of the record
294
     * @param	array		&$fieldArray: Array of field values
295
     * @param	\TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
296
     *
297
     * @return	void
298
     */
299
    public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, $pObj) {
300
301
        if ($status == 'update') {
302
303
            switch ($table) {
304
305
                // After database operations for table "tx_dlf_documents".
306
                case 'tx_dlf_documents':
307
308
                    // Delete/reindex document in Solr according to "hidden" status in database.
309
                    if (isset($fieldArray['hidden'])) {
310
311
                        // Get Solr core.
312
                        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
313
                            'tx_dlf_solrcores.uid',
314
                            'tx_dlf_solrcores,tx_dlf_documents',
315
                            'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore AND tx_dlf_documents.uid='.intval($id).tx_dlf_helper::whereClause('tx_dlf_solrcores'),
316
                            '',
317
                            '',
318
                            '1'
319
                        );
320
321
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
322
323
                            list ($core) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
324
325
                            if ($fieldArray['hidden']) {
326
327
                                // Establish Solr connection.
328
                                if ($solr = tx_dlf_solr::getInstance($core)) {
329
330
                                    // Delete Solr document.
331
                                    $solr->service->deleteByQuery('uid:'.$id);
0 ignored issues
show
Bug Best Practice introduced by
The property $service is declared protected in tx_dlf_solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
332
333
                                    $solr->service->commit();
334
335
                                }
336
337
                            } else {
338
339
                                // Reindex document.
340
                                $doc = & tx_dlf_document::getInstance($id);
341
342
                                if ($doc->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
343
344
                                    $doc->save($doc->pid, $core);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
345
346
                                } else {
347
348
                                    if (TYPO3_DLOG) {
0 ignored issues
show
Bug introduced by
The constant TYPO3_DLOG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
349
350
                                        \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_tcemain->processDatamap_afterDatabaseOperations('.$status.', '.$table.', '.$id.', [data], ['.get_class($pObj).'])] Failed to re-index document with UID "'.$id.'"', $this->extKey, SYSLOG_SEVERITY_ERROR, $fieldArray);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property extKey does not exist on tx_dlf_tcemain. Did you maybe forget to declare it?
Loading history...
351
352
                                    }
353
354
                                }
355
356
                            }
357
358
                        }
359
360
                    }
361
362
                    break;
363
364
            }
365
366
        }
367
368
    }
369
370
    /**
371
     * Post-processing hook for the process_cmdmap() method.
372
     *
373
     * @access	public
374
     *
375
     * @param	string		$command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete'
376
     * @param	string		$table: The destination table
377
     * @param	integer		$id: The uid of the record
378
     * @param	mixed		$value: The value for the command
379
     * @param	\TYPO3\CMS\Core\DataHandling\DataHandler $pObj: The parent object
380
     *
381
     * @return	void
382
     */
383
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj) {
384
385
        if (in_array($command, array ('move', 'delete', 'undelete')) && $table == 'tx_dlf_documents') {
386
387
            // Get Solr core.
388
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
389
                'tx_dlf_solrcores.uid',
390
                'tx_dlf_solrcores,tx_dlf_documents',
391
                'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore AND tx_dlf_documents.uid='.intval($id).tx_dlf_helper::whereClause('tx_dlf_solrcores'),
392
                '',
393
                '',
394
                '1'
395
            );
396
397
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
398
399
                list ($core) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
400
401
                switch ($command) {
402
403
                    case 'move':
404
                    case 'delete':
405
406
                        // Establish Solr connection.
407
                        if ($solr = tx_dlf_solr::getInstance($core)) {
408
409
                            // Delete Solr document.
410
                            $solr->service->deleteByQuery('uid:'.$id);
0 ignored issues
show
Bug Best Practice introduced by
The property $service is declared protected in tx_dlf_solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
411
412
                            $solr->service->commit();
413
414
                            if ($command == 'delete') {
415
416
                                break;
417
418
                            }
419
420
                        }
421
422
                    case 'undelete':
423
424
                        // Reindex document.
425
                        $doc = & tx_dlf_document::getInstance($id);
426
427
                        if ($doc->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
428
429
                            $doc->save($doc->pid, $core);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
430
431
                        } else {
432
433
                            if (TYPO3_DLOG) {
0 ignored issues
show
Bug introduced by
The constant TYPO3_DLOG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
434
435
                                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_tcemain->processCmdmap_postProcess('.$command.', '.$table.', '.$id.', '.$value.', ['.get_class($pObj).'])] Failed to re-index document with UID "'.$id.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property extKey does not exist on tx_dlf_tcemain. Did you maybe forget to declare it?
Loading history...
436
437
                            }
438
439
                        }
440
441
                        break;
442
443
                }
444
445
            }
446
447
        }
448
449
    }
450
451
}
452