Completed
Branch master (33af51)
by Timo
04:12
created

AdditionalFieldsIndexer::getFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.2894

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 12
cp 0.5833
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.2894
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
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
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
27
28
/**
29
 * Additional fields indexer.
30
 *
31
 * @todo Move this to an Index Queue frontend helper
32
 *
33
 * Adds page document fields as configured in
34
 * plugin.tx_solr.index.additionalFields.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class AdditionalFieldsIndexer implements SubstitutePageIndexer
39
{
40
41
    /**
42
     * @var array
43
     */
44
    protected $configuration;
45
46
    /**
47
     * @var array
48
     */
49
    protected $additionalIndexingFields = array();
50
51
    /**
52
     * @var array
53
     */
54
    protected $additionalFieldNames = array();
55
56
    /**
57
     * @param TypoScriptConfiguration $configuration
58
     */
59 1
    public function __construct(TypoScriptConfiguration $configuration = null)
60
    {
61 1
        $this->configuration = $configuration === null ? Util::getSolrConfiguration() : $configuration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configuration === null ...tion() : $configuration of type object<ApacheSolrForTypo...ypoScriptConfiguration> is incompatible with the declared type array of property $configuration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 1
        $this->additionalIndexingFields = $this->configuration->getIndexAdditionalFieldsConfiguration();
63 1
        $this->additionalFieldNames = $this->configuration->getIndexMappedAdditionalFieldNames();
64 1
    }
65
66
    /**
67
     * Returns a substitute document for the currently being indexed page.
68
     *
69
     * Uses the original document and adds fields as defined in
70
     * plugin.tx_solr.index.additionalFields.
71
     *
72
     * @param \Apache_Solr_Document $pageDocument The original page document.
73
     * @return \Apache_Solr_Document A Apache_Solr_Document object that replace the default page document
74
     */
75 1
    public function getPageDocument(\Apache_Solr_Document $pageDocument)
76
    {
77 1
        $substitutePageDocument = clone $pageDocument;
78 1
        $additionalFields = $this->getAdditionalFields();
79
80 1
        foreach ($additionalFields as $fieldName => $fieldValue) {
81 1
            if (!isset($pageDocument->{$fieldName})) {
82
                // making sure we only _add_ new fields
83 1
                $substitutePageDocument->setField($fieldName, $fieldValue);
84
            }
85
        }
86
87 1
        return $substitutePageDocument;
88
    }
89
90
    /**
91
     * Gets the additional fields as an array mapping field names to values.
92
     *
93
     * @return array An array mapping additional field names to their values.
94
     */
95 1
    protected function getAdditionalFields()
96
    {
97 1
        $additionalFields = array();
98
99 1
        foreach ($this->additionalFieldNames as $additionalFieldName) {
100 1
            $additionalFields[$additionalFieldName] = $this->getFieldValue($additionalFieldName);
101
        }
102
103 1
        return $additionalFields;
104
    }
105
106
    /**
107
     * Uses the page's cObj instance to resolve the additional field's value.
108
     *
109
     * @param string $fieldName The name of the field to get.
110
     * @return string The field's value.
111
     */
112 1
    protected function getFieldValue($fieldName)
113
    {
114
        // support for cObject if the value is a configuration
115 1
        if (is_array($this->additionalIndexingFields[$fieldName . '.'])) {
116 1
            $fieldValue = $GLOBALS['TSFE']->cObj->cObjGetSingle(
117 1
                $this->additionalIndexingFields[$fieldName],
118 1
                $this->additionalIndexingFields[$fieldName . '.']
119
            );
120
        } else {
121 1
            $fieldValue = $this->additionalIndexingFields[$fieldName];
122
        }
123
124 1
        return $fieldValue;
125
    }
126
}
127