Passed
Pull Request — master (#1308)
by Timo
22:26
created

ConfigurationService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 103
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setFlexFormService() 0 4 1
A setTypoScriptService() 0 4 1
A overrideConfigurationWithFlexFormSettings() 0 12 2
A overrideFilter() 0 19 2
A getFilterFromFlexForm() 0 15 3
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\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager as ExtbaseConfigurationManager;
21
use TYPO3\CMS\Extbase\Object\ObjectManager;
22
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
23
use TYPO3\CMS\Extbase\Service\FlexFormService;
24
use TYPO3\CMS\Extbase\Service\TypoScriptService;
25
26
/**
27
 * Service to ease work with configurations.
28
 *
29
 * @author Daniel Siepmann <[email protected]>
30
 */
31
class ConfigurationService
32
{
33
    /**
34
     * @var \TYPO3\CMS\Extbase\Service\FlexFormService
35
     * @inject
36
     */
37
    protected $flexFormService;
38
39
    /**
40
     * @var \TYPO3\CMS\Extbase\Service\TypoScriptService
41
     * @inject
42
     */
43
    protected $typoScriptService;
44
45
    /**
46
     * @param \TYPO3\CMS\Extbase\Service\FlexFormService $flexFormService
47
     */
48 1
    public function setFlexFormService($flexFormService)
49
    {
50 1
        $this->flexFormService = $flexFormService;
51 1
    }
52
53
    /**
54
     * @param \TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService
55
     */
56 1
    public function setTypoScriptService($typoScriptService)
57
    {
58 1
        $this->typoScriptService = $typoScriptService;
59 1
    }
60
61
    /**
62
     * Override the given solrConfiguration with flex form configuration.
63
     *
64
     * @param string $flexFormData The raw data from database.
65
     * @param TypoScriptConfiguration $solrTypoScriptConfiguration
66
     *
67
     * @return void
68
     */
69 29
    public function overrideConfigurationWithFlexFormSettings($flexFormData, TypoScriptConfiguration $solrTypoScriptConfiguration)
70
    {
71 29
        if (empty($flexFormData)) {
72 27
            return;
73
        }
74
75 2
        $flexFormConfiguration = $this->flexFormService->convertflexFormContentToArray($flexFormData);
76 2
        $flexFormConfiguration = $this->overrideFilter($flexFormConfiguration);
77 2
        $flexFormConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($flexFormConfiguration);
78
79 2
        $solrTypoScriptConfiguration->mergeSolrConfiguration($flexFormConfiguration, true, false);
80 2
    }
81
82
    /**
83
     * Override filter in configuration.
84
     *
85
     * Will parse the filter from flex form structure and rewrite it as typoscript structure.
86
     *
87
     * @param array $flexFormConfiguration
88
     *
89
     * @return array
90
     */
91 2
    protected function overrideFilter(array $flexFormConfiguration)
92
    {
93 2
        $filter = $this->getFilterFromFlexForm($flexFormConfiguration);
94 2
        unset($flexFormConfiguration['search']['query']['filter']);
95 2
        if (empty($filter)) {
96
            return $flexFormConfiguration;
97
        }
98
99 2
        return array_merge_recursive(
100
            $flexFormConfiguration,
101
            [
102
                'search' => [
103
                    'query' => [
104 2
                        'filter' => $filter,
105
                    ],
106
                ],
107
            ]
108
        );
109
    }
110
111
    /**
112
     * Returns filter in typoscript form from flex form.
113
     *
114
     * @param array $flexFormConfiguration
115
     *
116
     * @return array
117
     */
118 2
    protected function getFilterFromFlexForm(array $flexFormConfiguration)
119
    {
120 2
        $filterConfiguration = [];
121 2
        $filters = ObjectAccess::getPropertyPath($flexFormConfiguration, 'search.query.filter');
122
123 2
        if (empty($filters)) {
124
            return $filterConfiguration;
125
        }
126
127 2
        foreach ($filters as $filter) {
128 2
            $filter = $filter['field'];
129 2
            $filterConfiguration[] = $filter['field'] . ':' . $filter['value'];
130
        }
131 2
        return $filterConfiguration;
132
    }
133
}
134