Passed
Push — master ( f63716...615686 )
by Timo
11:21 queued 06:56
created

ConfigurationService::setTypoScriptService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
18
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
19
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager as ExtbaseConfigurationManager;
22
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
23
use TYPO3\CMS\Extbase\Service\FlexFormService;
24
25
/**
26
 * Service to ease work with configurations.
27
 *
28
 * @author Daniel Siepmann <[email protected]>
29
 */
30
class ConfigurationService
31
{
32
    /**
33
     * @var FlexFormService
34
     */
35
    protected $flexFormService;
36
37
    /**
38
     * @var TypoScriptService
39
     */
40
    protected $typoScriptService;
41
42
    public function __construct()
43
    {
44
        $this->flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
45
        $this->typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
46
    }
47
48 1
    /**
49
     * @param FlexFormService $flexFormService
50 1
     */
51 1
    public function setFlexFormService($flexFormService)
52
    {
53
        $this->flexFormService = $flexFormService;
54
    }
55
56 1
    /**
57
     * @param \TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService
58 1
     */
59 1
    public function setTypoScriptService($typoScriptService)
60
    {
61
        $this->typoScriptService = $typoScriptService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $typoScriptService of type object<TYPO3\CMS\Extbase...vice\TypoScriptService> is incompatible with the declared type object<TYPO3\CMS\Core\Ty...ript\TypoScriptService> of property $typoScriptService.

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
    }
63
64
    /**
65
     * Override the given solrConfiguration with flex form configuration.
66
     *
67
     * @param string $flexFormData The raw data from database.
68
     * @param TypoScriptConfiguration $solrTypoScriptConfiguration
69 34
     *
70
     * @return void
71 34
     */
72 32
    public function overrideConfigurationWithFlexFormSettings($flexFormData, TypoScriptConfiguration $solrTypoScriptConfiguration)
73
    {
74
        if (empty($flexFormData)) {
75 2
            return;
76 2
        }
77 2
78
        $flexFormConfiguration = $this->flexFormService->convertFlexFormContentToArray($flexFormData);
79 2
        $flexFormConfiguration = $this->overrideFilter($flexFormConfiguration);
80 2
        $flexFormConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($flexFormConfiguration);
81
82
        $solrTypoScriptConfiguration->mergeSolrConfiguration($flexFormConfiguration, true, false);
83
    }
84
85
    /**
86
     * Override filter in configuration.
87
     *
88
     * Will parse the filter from flex form structure and rewrite it as typoscript structure.
89
     *
90
     * @param array $flexFormConfiguration
91 2
     *
92
     * @return array
93 2
     */
94 2
    protected function overrideFilter(array $flexFormConfiguration)
95 2
    {
96
        $filter = $this->getFilterFromFlexForm($flexFormConfiguration);
97
        unset($flexFormConfiguration['search']['query']['filter']);
98
        if (empty($filter)) {
99 2
            return $flexFormConfiguration;
100
        }
101
102
        return array_merge_recursive(
103
            $flexFormConfiguration,
104 2
            [
105
                'search' => [
106
                    'query' => [
107
                        'filter' => $filter,
108
                    ],
109
                ],
110
            ]
111
        );
112
    }
113
114
    /**
115
     * Returns filter in typoscript form from flex form.
116
     *
117
     * @param array $flexFormConfiguration
118 2
     *
119
     * @return array
120 2
     */
121 2
    protected function getFilterFromFlexForm(array $flexFormConfiguration)
122
    {
123 2
        $filterConfiguration = [];
124
        $filters = ObjectAccess::getPropertyPath($flexFormConfiguration, 'search.query.filter');
125
126
        if (empty($filters)) {
127 2
            return $filterConfiguration;
128 2
        }
129 2
130
        foreach ($filters as $filter) {
131 2
            $filter = $filter['field'];
132
            $filterConfiguration[] = $filter['field'] . ':' . $filter['value'];
133
        }
134
        return $filterConfiguration;
135
    }
136
}
137