Passed
Push — master ( f63a3e...201a83 )
by Timo
34:43
created

PageFieldMappingIndexer::resolveFieldValue()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.2834

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 19
cp 0.6842
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
crap 3.2834
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[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 2 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
// TODO use/extend ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
28
use ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer;
29
use ApacheSolrForTypo3\Solr\IndexQueue\InvalidFieldNameException;
30
use ApacheSolrForTypo3\Solr\SubstitutePageIndexer;
31
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
32
use ApacheSolrForTypo3\Solr\Util;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35
36
/**
37
 * Indexer to add / overwrite page document fields as defined in
38
 * plugin.tx_solr.index.queue.pages.fields.
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 */
42
class PageFieldMappingIndexer implements SubstitutePageIndexer
43
{
44
    /**
45
     * @var \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration
46
     */
47
    protected $configuration;
48
49
    /**
50
     * @var string
51
     */
52
    protected $pageIndexingConfigurationName = 'pages';
53
54
    /**
55
     * @param TypoScriptConfiguration $configuration
56
     */
57 6
    public function __construct(TypoScriptConfiguration $configuration = null)
58
    {
59 6
        $this->configuration = $configuration == null ? Util::getSolrConfiguration() : $configuration;
60 6
    }
61
62
    /**
63
     * @param string $pageIndexingConfigurationName
64
     */
65 6
    public function setPageIndexingConfigurationName($pageIndexingConfigurationName)
66
    {
67 6
        $this->pageIndexingConfigurationName = $pageIndexingConfigurationName;
68 6
    }
69
70
    /**
71
     * Returns a substitute document for the currently being indexed page.
72
     *
73
     * Uses the original document and adds fields as defined in
74
     * plugin.tx_solr.index.queue.pages.fields.
75
     *
76
     * @param \Apache_Solr_Document $pageDocument The original page document.
77
     * @return \Apache_Solr_Document A Apache_Solr_Document object that replace the default page document
78
     */
79 6
    public function getPageDocument(\Apache_Solr_Document $pageDocument)
80
    {
81 6
        $substitutePageDocument = clone $pageDocument;
82
83
84 6
        $mappedFields = $this->getMappedFields();
85 6
        foreach ($mappedFields as $fieldName => $fieldValue) {
86 6
            if (isset($substitutePageDocument->{$fieldName})) {
87
                // reset = overwrite, especially important to not make fields
88
                // multi valued where they may not accept multiple values
89
                unset($substitutePageDocument->{$fieldName});
90
            }
91
92
            // add new field / overwrite field if it was set before
93 6
            if ($fieldValue !== '' && $fieldValue !== null) {
94 6
                $substitutePageDocument->setField($fieldName, $fieldValue);
95
            }
96
        }
97
98 6
        return $substitutePageDocument;
99
    }
100
101
    /**
102
     * Gets the mapped fields as an array mapping field names to values.
103
     *
104
     * @throws InvalidFieldNameException
105
     * @return array An array mapping field names to their values.
106
     */
107 6
    protected function getMappedFields()
108
    {
109 6
        $fields = [];
110
111 6
        $mappedFieldNames = $this->configuration->getIndexQueueMappedFieldsByConfigurationName($this->pageIndexingConfigurationName);
112
113 6
        foreach ($mappedFieldNames as $mappedFieldName) {
114 6
            if (!AbstractIndexer::isAllowedToOverrideField($mappedFieldName)) {
115
                throw new InvalidFieldNameException(
116
                    'Must not overwrite field "type".',
117
                    1435441863
118
                );
119
            }
120 6
            $fields[$mappedFieldName] = $this->resolveFieldValue($mappedFieldName);
121
        }
122
123 6
        return $fields;
124
    }
125
126
    /**
127
     * Resolves a field mapping to its value depending on its configuration.
128
     *
129
     * Allows to put the page record through cObj processing if wanted / needed.
130
     * Otherwise the plain page record field value is used.
131
     *
132
     * @param string $solrFieldName The Solr field name to resolve the value from the item's record
133
     * @return string The resolved string value to be indexed
134
     */
135 6
    protected function resolveFieldValue($solrFieldName)
136
    {
137 6
        $pageRecord = $GLOBALS['TSFE']->page;
138
139 6
        $pageIndexingConfiguration = $this->configuration->getIndexQueueFieldsConfigurationByConfigurationName($this->pageIndexingConfigurationName);
140
141 6
        if (isset($pageIndexingConfiguration[$solrFieldName . '.'])) {
142
            // configuration found => need to resolve a cObj
143 5
            $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
144 5
            $contentObject->start($pageRecord, 'pages');
145
146 5
            $fieldValue = $contentObject->cObjGetSingle(
147 5
                $pageIndexingConfiguration[$solrFieldName],
148 5
                $pageIndexingConfiguration[$solrFieldName . '.']
149
            );
150
151 5
            if (AbstractIndexer::isSerializedValue($pageIndexingConfiguration, $solrFieldName)) {
152 5
                $fieldValue = unserialize($fieldValue);
153
            }
154
        } else {
155 5
            $fieldValue = $pageRecord[$pageIndexingConfiguration[$solrFieldName]];
156
        }
157
158 6
        return $fieldValue;
159
    }
160
}
161