Passed
Push — master ( efbfe4...f2d6e9 )
by Timo
23:10
created

getParsedSolrFieldsFromSchema()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2.0157

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 19
cp 0.8421
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 1
nop 2
crap 2.0157
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\UserFunctions;
3
4
/*
5
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
 * 02110-1301, USA.
21
 */
22
23
use ApacheSolrForTypo3\Solr\ConnectionManager;
24
use ApacheSolrForTypo3\Solr\Util;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * This class contains all user functions for flexforms.
29
 *
30
 * @author Daniel Siepmann <[email protected]>
31
 */
32
class FlexFormUserFunctions
33
{
34
    /**
35
     * Provides all facet fields for a flexform select, enabling the editor to select one of them.
36
     *
37
     * @param array $parentInformation
38
     *
39
     * @return void
40
     */
41 3
    public function getFacetFieldsFromSchema(array &$parentInformation)
42
    {
43 3
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
44 3
        $configuredFacets = $this->getConfiguredFacetsForPage($pageRecord['pid']);
45
46 3
        if (!is_array($pageRecord)) {
47 1
            $parentInformation['items'] = [];
48 1
            return;
49
        }
50
51 2
        $newItems = $this->getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord);
52 2
        $parentInformation['items'] = $newItems;
53 2
    }
54
55
    /**
56
     * This method parses the solr schema fields into the required format for the backend flexform.
57
     *
58
     * @param array $configuredFacets
59
     * @param array $pageRecord
60
     * @return mixed
61
     */
62 2
    protected function getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord)
63
    {
64 2
        $newItems = [];
65
66 2
        array_map(function($fieldName) use (&$newItems, $configuredFacets) {
67 2
            $value = $fieldName;
68 2
            $label = $fieldName;
69
70 2
            $facetNameFilter = function($facet) use ($fieldName) {
71 1
                return ($facet['field'] === $fieldName);
72 2
            };
73 2
            $configuredFacets = array_filter($configuredFacets, $facetNameFilter);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $configuredFacets, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
74 2
            if (!empty($configuredFacets)) {
75 1
                $configuredFacet = array_values($configuredFacets);
76 1
                $label = $configuredFacet[0]['label'];
77
            }
78
79 2
            $newItems[$label] = [$label, $value];
80 2
        }, $this->getFieldNamesFromSolrMetaDataForPage($pageRecord));
81
82 2
        ksort($newItems, SORT_NATURAL);
83 2
        return $newItems;
84
    }
85
    /**
86
     * Retrieves the configured facets for a page.
87
     *
88
     * @param integer $pid
89
     * @return array
90
     */
91
    protected function getConfiguredFacetsForPage($pid)
92
    {
93
        $typoScriptConfiguration = $this->getConfigurationFromPageId($pid);
94
        return $typoScriptConfiguration->getSearchFacetingFacets();
95
    }
96
97
    /**
98
     * Get solr connection.
99
     *
100
     * @param array $pageRecord
101
     *
102
     * @return \ApacheSolrForTypo3\Solr\SolrService
103
     */
104
    protected function getConnection(array $pageRecord)
105
    {
106
        return GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageRecord['pid'], $pageRecord['sys_language_uid']);
107
    }
108
109
    /**
110
     * Retrieves all fieldnames that occure in the solr schema for one page.
111
     *
112
     * @param array $pageRecord
113
     * @return array
114
     */
115
    protected function getFieldNamesFromSolrMetaDataForPage(array $pageRecord)
116
    {
117
        return array_keys((array)$this->getConnection($pageRecord)->getFieldsMetaData());
118
    }
119
120
    /**
121
     * @param array $parentInformation
122
     */
123 1
    public function getAvailableTemplates(array &$parentInformation)
124
    {
125 1
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
126 1
        if (!is_array($pageRecord) || !isset ($pageRecord['pid'])) {
127
            $parentInformation['items'] = [];
128
            return;
129
        }
130
131 1
        $pageId = $pageRecord['pid'];
132
133 1
        $templateKey = $this->getTypoScriptTemplateKeyFromFieldName($parentInformation);
134 1
        $availableTemplate = $this->getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey);
135 1
        $newItems = $this->buildSelectItemsFromAvailableTemplate($availableTemplate);
136
137 1
        $parentInformation['items'] = $newItems;
138 1
    }
139
140
    /**
141
     * @param array $parentInformation
142
     * @return string
143
     */
144 1
    protected function getTypoScriptTemplateKeyFromFieldName(array &$parentInformation)
145
    {
146 1
        $field = $parentInformation['field'];
147 1
        return str_replace('view.templateFiles.', '', $field);
148
    }
149
150
    /**
151
     * @param $pid
152
     * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration|array
153
     */
154
    protected function getConfigurationFromPageId($pid)
155
    {
156
        $typoScriptConfiguration = Util::getSolrConfigurationFromPageId($pid);
157
        return $typoScriptConfiguration;
158
    }
159
160
    /**
161
     * Retrieves the configured templates from TypoScript.
162
     *
163
     * @param integer $pageId
164
     * @param string $templateKey
165
     * @return array
166
     */
167
    protected function getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey)
168
    {
169
        $configuration = $this->getConfigurationFromPageId($pageId);
170
        return $configuration->getAvailableTemplatesByFileKey($templateKey);
171
    }
172
173
    /**
174
     * Returns the available templates as needed for the flexform.
175
     *
176
     * @param array $availableTemplates
177
     * @return array
178
     */
179 1
    protected function buildSelectItemsFromAvailableTemplate($availableTemplates)
180
    {
181 1
        $newItems = [];
182 1
        $newItems['Use Default'] = ['Use Default', null];
183 1
        foreach ($availableTemplates as $availableTemplate) {
184 1
            $label = isset($availableTemplate['label']) ? $availableTemplate['label'] : '';
185 1
            $value = isset($availableTemplate['file']) ? $availableTemplate['file'] : '';
186 1
            $newItems[$label] = [$label, $value];
187
        }
188
189 1
        return $newItems;
190
    }
191
}
192