Passed
Pull Request — main (#3077)
by Christoph
45:40
created

Service::processDocument()   C

Complexity

Conditions 13
Paths 34

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 16.7577

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 62
ccs 23
cts 32
cp 0.7188
rs 6.6166
c 0
b 0
f 0
cc 13
nc 34
nop 2
crap 16.7577

How to fix   Long Method    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
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\FieldProcessor;
19
20
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
21
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
22
use Doctrine\DBAL\Exception as DBALException;
23
use Exception;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Service class that modifies fields in an Apache Solr Document, used for
28
 * common field processing during indexing or resolving
29
 *
30
 * @author Daniel Poetzinger <[email protected]>
31
 * @copyright (c) 2009-2015 Daniel Poetzinger <[email protected]>
32
 */
33
class Service
34
{
35
    /**
36
     * Modifies a list of documents
37
     *
38
     * @param Document[] $documents
39
     * @param array $processingConfiguration
40
     * @throws DBALDriverException
41
     * @throws DBALException
42 50
     */
43 50
    public function processDocuments(array $documents, array $processingConfiguration)
44 50
    {
45
        foreach ($documents as $document) {
46 50
            $this->processDocument($document, $processingConfiguration);
47
        }
48
    }
49
50
    /**
51
     * modifies a document according to the given configuration
52
     *
53
     * @param Document $document
54 54
     * @param array $processingConfiguration
55 54
     * @throws DBALDriverException
56 54
     * @throws DBALException
57 54
     */
58
    public function processDocument(Document $document, array $processingConfiguration)
59 54
    {
60 54
        foreach ($processingConfiguration as $fieldName => $instruction) {
61
            $fieldValue = $document[$fieldName] ?? false;
62 52
            $isSingleValueField = false;
63 52
64
            if ($fieldValue !== false) {
65
                if (!is_array($fieldValue)) {
66 54
                    // turn single value field into multi value field
67 54
                    $fieldValue = [$fieldValue];
68
                    $isSingleValueField = true;
69
                }
70
71
                switch ($instruction) {
72 54
                    case 'timestampToUtcIsoDate':
73
                        /** @var $processor TimestampToUtcIsoDate */
74 52
                        $processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class);
75 52
                        $fieldValue = $processor->process($fieldValue);
76 52
                        break;
77 33
                    case 'timestampToIsoDate':
78
                        /** @var $processor TimestampToIsoDate */
79
                        $processor = GeneralUtility::makeInstance(TimestampToIsoDate::class);
80
                        $fieldValue = $processor->process($fieldValue);
81
                        break;
82 33
                    case 'pathToHierarchy':
83
                        /** @var $processor PathToHierarchy */
84 31
                        $processor = GeneralUtility::makeInstance(PathToHierarchy::class);
85 31
                        $fieldValue = $processor->process($fieldValue);
86 31
                        break;
87 2
                    case 'pageUidToHierarchy':
88
                        /** @var $processor PageUidToHierarchy */
89
                        $processor = GeneralUtility::makeInstance(PageUidToHierarchy::class);
90
                        $fieldValue = $processor->process($fieldValue);
91
                        break;
92 2
                    case 'categoryUidToHierarchy':
93 2
                        /** @var $processor CategoryUidToHierarchy */
94 2
                        $processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class);
95
                        $fieldValue = $processor->process($fieldValue);
96
                        break;
97 54
                    case 'uppercase':
98
                        $fieldValue = array_map('mb_strtoupper', $fieldValue);
99 52
                        break;
100
                    default:
101
                        $classReference = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['fieldProcessor'][$instruction] ?? false;
102 54
                        if ($classReference) {
103
                            $customFieldProcessor = GeneralUtility::makeInstance($classReference);
104
                            if ($customFieldProcessor instanceof FieldProcessor) {
105 54
                                $fieldValue = $customFieldProcessor->process($fieldValue);
106
                            } else {
107
                                throw new Exception('A FieldProcessor must implement the FieldProcessor interface', 1635082295);
108
                            }
109
                        } else {
110
                            throw new Exception(sprintf('FieldProcessor %s is not implemented', $instruction), 1635082296);
111
                        }
112
                }
113
114
                if ($isSingleValueField) {
115
                    // turn multi value field back into single value field
116
                    $fieldValue = $fieldValue[0];
117
                }
118
119
                $document->setField($fieldName, $fieldValue);
120
            }
121
        }
122
    }
123
}
124