Passed
Push — master ( 89ac8f...d970e3 )
by Timo
20:33
created

Service   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 13
eloc 37
dl 0
loc 70
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 3 2
B processDocument() 0 49 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 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 86
    public function processDocuments(array $documents, array $processingConfiguration) {
51 86
        foreach ($documents as $document) {
52 86
            $this->processDocument($document, $processingConfiguration);
53
        }
54 86
    }
55
56
    /**
57
     * modifies a document according to the given configuration
58
     *
59
     * @param Document $document
60
     * @param array $processingConfiguration
61
     */
62 90
    public function processDocument(Document $document, array $processingConfiguration) {
63 90
        foreach ($processingConfiguration as $fieldName => $instruction) {
64 90
            $fieldValue = $document[$fieldName] ?? false;
65 90
            $isSingleValueField = false;
66
67 90
            if ($fieldValue !== false) {
68 90
                if (!is_array($fieldValue)) {
69
                    // turn single value field into multi value field
70 88
                    $fieldValue = [$fieldValue];
71 88
                    $isSingleValueField = true;
72
                }
73
74 90
                switch ($instruction) {
75 90
                    case 'timestampToUtcIsoDate':
76
                        /** @var $processor TimestampToUtcIsoDate */
77
                        $processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class);
78
                        $fieldValue = $processor->process($fieldValue);
79
                        break;
80 90
                    case 'timestampToIsoDate':
81
                        /** @var $processor TimestampToIsoDate */
82 88
                        $processor = GeneralUtility::makeInstance(TimestampToIsoDate::class);
83 88
                        $fieldValue = $processor->process($fieldValue);
84 88
                        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 90
                if ($isSingleValueField) {
106
                    // turn multi value field back into single value field
107 88
                    $fieldValue = $fieldValue[0];
108
                }
109
110 90
                $document->setField($fieldName, $fieldValue);
111
            }
112
        }
113 90
    }
114
}
115