Passed
Pull Request — release-11.2.x (#3528)
by Rafael
09:36
created

Service::processDocument()   B

Complexity

Conditions 11
Paths 30

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 13.2441

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 49
ccs 25
cts 34
cp 0.7352
rs 7.3166
cc 11
nc 30
nop 2
crap 13.2441

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 ApacheSolrForTypo3\Solr\FieldProcessor;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
18
use ApacheSolrForTypo3\Solr\FieldProcessor\CategoryUidToHierarchy;
19
use ApacheSolrForTypo3\Solr\FieldProcessor\PageUidToHierarchy;
20
use ApacheSolrForTypo3\Solr\FieldProcessor\PathToHierarchy;
21
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToIsoDate;
22
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToUtcIsoDate;
23
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Service class that modifies fields in a 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
    /**
37
     * Modifies a list of documents
38
     *
39
     * @param Document[] $documents
40
     * @param array $processingConfiguration
41
     */
42 57
    public function processDocuments(array $documents, array $processingConfiguration) {
43 57
        foreach ($documents as $document) {
44 57
            $this->processDocument($document, $processingConfiguration);
45
        }
46 57
    }
47
48
    /**
49
     * modifies a document according to the given configuration
50
     *
51
     * @param Document $document
52
     * @param array $processingConfiguration
53
     */
54 61
    public function processDocument(Document $document, array $processingConfiguration) {
55 61
        foreach ($processingConfiguration as $fieldName => $instruction) {
56 61
            $fieldValue = $document[$fieldName] ?? false;
57 61
            $isSingleValueField = false;
58
59 61
            if ($fieldValue !== false) {
60 61
                if (!is_array($fieldValue)) {
61
                    // turn single value field into multi value field
62 59
                    $fieldValue = [$fieldValue];
63 59
                    $isSingleValueField = true;
64
                }
65
66
                switch ($instruction) {
67 61
                    case 'timestampToUtcIsoDate':
68
                        /** @var $processor TimestampToUtcIsoDate */
69
                        $processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class);
70
                        $fieldValue = $processor->process($fieldValue);
71
                        break;
72 61
                    case 'timestampToIsoDate':
73
                        /** @var $processor TimestampToIsoDate */
74 59
                        $processor = GeneralUtility::makeInstance(TimestampToIsoDate::class);
75 59
                        $fieldValue = $processor->process($fieldValue);
76 59
                        break;
77 39
                    case 'pathToHierarchy':
78
                        /** @var $processor PathToHierarchy */
79
                        $processor = GeneralUtility::makeInstance(PathToHierarchy::class);
80
                        $fieldValue = $processor->process($fieldValue);
81
                        break;
82 39
                    case 'pageUidToHierarchy':
83
                        /** @var $processor PageUidToHierarchy */
84 37
                        $processor = GeneralUtility::makeInstance(PageUidToHierarchy::class);
85 37
                        $fieldValue = $processor->process($fieldValue);
86 37
                        break;
87 2
                    case 'categoryUidToHierarchy':
88
                        /** @var $processor CategoryUidToHierarchy */
89
                        $processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class);
90
                        $fieldValue = $processor->process($fieldValue);
91
                        break;
92 2
                    case 'uppercase':
93 2
                        $fieldValue = array_map('mb_strtoupper', $fieldValue);
94 2
                        break;
95
                }
96
97 61
                if ($isSingleValueField) {
98
                    // turn multi value field back into single value field
99 59
                    $fieldValue = $fieldValue[0];
100
                }
101
102 61
                $document->setField($fieldName, $fieldValue);
103
            }
104
        }
105 61
    }
106
}
107