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

Service   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 78
ccs 36
cts 39
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processDocuments() 0 6 2
C processDocument() 0 54 11
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