Passed
Push — master ( 9a88de...e5ef09 )
by Timo
21:32
created

Service::processDocument()   C

Complexity

Conditions 11
Paths 30

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 11.0761

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 32
cts 35
cp 0.9143
rs 6.6153
c 0
b 0
f 0
cc 11
eloc 35
nc 30
nop 2
crap 11.0761

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
namespace ApacheSolrForTypo3\Solr\FieldProcessor;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Daniel Poetzinger <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\FieldProcessor\CategoryUidToHierarchy;
28
use ApacheSolrForTypo3\Solr\FieldProcessor\PageUidToHierarchy;
29
use ApacheSolrForTypo3\Solr\FieldProcessor\PathToHierarchy;
30
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToIsoDate;
31
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToUtcIsoDate;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * Service class that modifies fields in a Apache_Solr_Document, used for
36
 * common field processing during indexing or resolving
37
 *
38
 * @author Daniel Poetzinger <[email protected]>
39
 */
40
class Service
41
{
42
43
    /**
44
     * Modifies a list of documents
45
     *
46
     * @param \Apache_Solr_Document[] $documents
47
     * @param array $processingConfiguration
48
     */
49 85
    public function processDocuments(
50
        array $documents,
51
        array $processingConfiguration
52
    ) {
53 85
        foreach ($documents as $document) {
54 85
            $this->processDocument($document, $processingConfiguration);
55
        }
56 85
    }
57
58
    /**
59
     * modifies a document according to the given configuration
60
     *
61
     * @param \Apache_Solr_Document $document
62
     * @param array $processingConfiguration
63
     */
64 89
    public function processDocument(
65
        \Apache_Solr_Document $document,
66
        array $processingConfiguration
67
    ) {
68 89
        foreach ($processingConfiguration as $fieldName => $instruction) {
69 89
            $fieldInformation = $document->getField($fieldName);
70 89
            $isSingleValueField = false;
71
72 89
            if ($fieldInformation !== false) {
73 89
                $fieldValue = $fieldInformation['value'];
74
75 89
                if (!is_array($fieldValue)) {
76
                    // turn single value field into multi value field
77 87
                    $fieldValue = [$fieldValue];
78 87
                    $isSingleValueField = true;
79
                }
80
81
                switch ($instruction) {
82 89
                    case 'timestampToUtcIsoDate':
83
                        /** @var $processor TimestampToUtcIsoDate */
84
                        $processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class);
85
                        $fieldValue = $processor->process($fieldValue);
86
                        break;
87 89
                    case 'timestampToIsoDate':
88
                        /** @var $processor TimestampToIsoDate */
89 87
                        $processor = GeneralUtility::makeInstance(TimestampToIsoDate::class);
90 87
                        $fieldValue = $processor->process($fieldValue);
91 87
                        break;
92 68
                    case 'pathToHierarchy':
93
                        /** @var $processor PathToHierarchy */
94 1
                        $processor = GeneralUtility::makeInstance(PathToHierarchy::class);
95 1
                        $fieldValue = $processor->process($fieldValue);
96 1
                        break;
97 67
                    case 'pageUidToHierarchy':
98
                        /** @var $processor PageUidToHierarchy */
99 65
                        $processor = GeneralUtility::makeInstance(PageUidToHierarchy::class);
100 65
                        $fieldValue = $processor->process($fieldValue);
101 65
                        break;
102 34
                    case 'categoryUidToHierarchy':
103
                        /** @var $processor CategoryUidToHierarchy */
104 32
                        $processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class);
105 32
                        $fieldValue = $processor->process($fieldValue);
106 32
                        break;
107 2
                    case 'uppercase':
108 2
                        $fieldValue = array_map('mb_strtoupper', $fieldValue);
109 2
                        break;
110
                }
111
112 89
                if ($isSingleValueField) {
113
                    // turn multi value field back into single value field
114 87
                    $fieldValue = $fieldValue[0];
115
                }
116
117 89
                $document->setField($fieldName, $fieldValue);
118
            }
119
        }
120 89
    }
121
}
122