Failed Conditions
Pull Request — master (#3000)
by
unknown
37:55
created

AdditionalFieldsIndexer::getFieldValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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 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
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
27
use ApacheSolrForTypo3\Solr\System\ContentObject\ContentObjectService;
28
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Additional fields indexer.
33
 *
34
 * @todo Move this to an Index Queue frontend helper
35
 *
36
 * Adds page document fields as configured in
37
 * plugin.tx_solr.index.additionalFields.
38
 *
39
 * @author Ingo Renner <[email protected]>
40
 */
41
class AdditionalFieldsIndexer implements SubstitutePageIndexer
42
{
43
44
    /**
45
     * @var TypoScriptConfiguration
46
     */
47
    protected $configuration;
48
49
    /**
50
     * @var array
51
     */
52
    protected $additionalIndexingFields = [];
53
54
    /**
55
     * @var array
56
     */
57
    protected $additionalFieldNames = [];
58
59
    /**
60
     * @var ContentObjectService
61
     */
62
    protected $contentObjectService = null;
63
64
    /**
65
     * @param TypoScriptConfiguration $configuration
66
     * @param ContentObjectService $contentObjectService
67
     */
68
    public function __construct(TypoScriptConfiguration $configuration = null, ContentObjectService $contentObjectService = null)
69
    {
70
        $this->configuration = $configuration === null ? Util::getSolrConfiguration() : $configuration;
71
        $this->additionalIndexingFields = $this->configuration->getIndexAdditionalFieldsConfiguration();
72
        $this->additionalFieldNames = $this->configuration->getIndexMappedAdditionalFieldNames();
73
        $this->contentObjectService = $contentObjectService === null ? GeneralUtility::makeInstance(ContentObjectService::class) : $contentObjectService;
74
    }
75
76
    /**
77
     * Returns a substitute document for the currently being indexed page.
78
     *
79
     * Uses the original document and adds fields as defined in
80
     * plugin.tx_solr.index.additionalFields.
81
     *
82
     * @param Document $pageDocument The original page document.
83
     * @return Document A Apache Solr Document object that replace the default page document
84
     */
85
    public function getPageDocument(Document $pageDocument)
86
    {
87
        $substitutePageDocument = clone $pageDocument;
88
        $additionalFields = $this->getAdditionalFields();
89
90
        foreach ($additionalFields as $fieldName => $fieldValue) {
91
            if (!isset($pageDocument->{$fieldName})) {
92
                // making sure we only _add_ new fields
93
                $substitutePageDocument->setField($fieldName, $fieldValue);
94
            }
95
        }
96
97
        return $substitutePageDocument;
98
    }
99
100
    /**
101
     * Gets the additional fields as an array mapping field names to values.
102
     *
103
     * @return array An array mapping additional field names to their values.
104
     */
105
    protected function getAdditionalFields()
106
    {
107
        $additionalFields = [];
108
109
        foreach ($this->additionalFieldNames as $additionalFieldName) {
110
            $additionalFields[$additionalFieldName] = $this->getFieldValue($additionalFieldName);
111
        }
112
113
        return $additionalFields;
114
    }
115
116
    /**
117
     * Uses the page's cObj instance to resolve the additional field's value.
118
     *
119
     * @param string $fieldName The name of the field to get.
120
     * @return string The field's value.
121
     */
122
    protected function getFieldValue($fieldName)
123
    {
124
        return $this->contentObjectService->renderSingleContentObjectByArrayAndKey($this->additionalIndexingFields, $fieldName);
125
    }
126
}
127