Passed
Push — master ( 14b490...3d7c7d )
by Timo
23:43
created

Service::processDocument()   B

Complexity

Conditions 11
Paths 30

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 11.083

Importance

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

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
 *  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 ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Service class that modifies fields in a Apache Solr Document, used for
37
 * common field processing during indexing or resolving
38
 *
39
 * @author Daniel Poetzinger <[email protected]>
40
 */
41
class Service
42
{
43
44
    /**
45
     * Modifies a list of documents
46
     *
47
     * @param Document[] $documents
48
     * @param array $processingConfiguration
49
     */
50 88
    public function processDocuments(array $documents, array $processingConfiguration) {
51 88
        foreach ($documents as $document) {
52 88
            $this->processDocument($document, $processingConfiguration);
53
        }
54 88
    }
55
56
    /**
57
     * modifies a document according to the given configuration
58
     *
59
     * @param Document $document
60
     * @param array $processingConfiguration
61
     */
62 92
    public function processDocument(Document $document, array $processingConfiguration) {
63 92
        foreach ($processingConfiguration as $fieldName => $instruction) {
64 92
            $fieldValue = $document[$fieldName] ?? false;
65 92
            $isSingleValueField = false;
66
67 92
            if ($fieldValue !== false) {
68 92
                if (!is_array($fieldValue)) {
69
                    // turn single value field into multi value field
70 90
                    $fieldValue = [$fieldValue];
71 90
                    $isSingleValueField = true;
72
                }
73
74
                switch ($instruction) {
75 92
                    case 'timestampToUtcIsoDate':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76
                        /** @var $processor TimestampToUtcIsoDate */
77
                        $processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class);
78
                        $fieldValue = $processor->process($fieldValue);
79
                        break;
80 92
                    case 'timestampToIsoDate':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
81
                        /** @var $processor TimestampToIsoDate */
82 90
                        $processor = GeneralUtility::makeInstance(TimestampToIsoDate::class);
83 90
                        $fieldValue = $processor->process($fieldValue);
84 90
                        break;
85 70
                    case 'pathToHierarchy':
86
                        /** @var $processor PathToHierarchy */
87 1
                        $processor = GeneralUtility::makeInstance(PathToHierarchy::class);
88 1
                        $fieldValue = $processor->process($fieldValue);
89 1
                        break;
90 69
                    case 'pageUidToHierarchy':
91
                        /** @var $processor PageUidToHierarchy */
92 67
                        $processor = GeneralUtility::makeInstance(PageUidToHierarchy::class);
93 67
                        $fieldValue = $processor->process($fieldValue);
94 67
                        break;
95 36
                    case 'categoryUidToHierarchy':
96
                        /** @var $processor CategoryUidToHierarchy */
97 34
                        $processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class);
98 34
                        $fieldValue = $processor->process($fieldValue);
99 34
                        break;
100 2
                    case 'uppercase':
101 2
                        $fieldValue = array_map('mb_strtoupper', $fieldValue);
102 2
                        break;
103
                }
104
105 92
                if ($isSingleValueField) {
106
                    // turn multi value field back into single value field
107 90
                    $fieldValue = $fieldValue[0];
108
                }
109
110 92
                $document->setField($fieldName, $fieldValue);
111
            }
112
        }
113 92
    }
114
}
115