ConfigurationManager::getTypoScriptConfiguration()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 7.6666
cc 10
nc 64
nop 4
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Configuration;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2016 Timo Schmidt <[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
27
use TYPO3\CMS\Core\SingletonInterface;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Configuration manager old the configuration instance.
32
 * Singleton
33
 *
34
 * @author Timo Schmidt <[email protected]>
35
 */
36
class ConfigurationManager implements SingletonInterface
37
{
38
    /**
39
     * TypoScript Configurations
40
     *
41
     * @var array
42
     */
43
    protected $typoScriptConfigurations = [];
44
45
    /**
46
     * Resets the state of the configuration manager.
47
     *
48
     * @return void
49
     */
50 19
    public function reset()
51
    {
52 19
        $this->typoScriptConfigurations = [];
53 19
    }
54
55
    /**
56
     * Retrieves the TypoScriptConfiguration object from an configuration array, pageId, languageId and TypoScript
57
     * path that is used in in the current context.
58
     *
59
     * @param array $configurationArray
60
     * @param int $contextPageId
61
     * @param int $contextLanguageId
62
     * @param string $contextTypoScriptPath
63
     * @return TypoScriptConfiguration
64
     */
65 235
    public function getTypoScriptConfiguration(array $configurationArray = null, $contextPageId = null, $contextLanguageId = 0, $contextTypoScriptPath = '')
66
    {
67 235
        if ($configurationArray == null) {
68 176
            if (isset($this->typoScriptConfigurations['default'])) {
69 81
                $configurationArray = $this->typoScriptConfigurations['default'];
70
            } else {
71 173
                if (!empty($GLOBALS['TSFE']->tmpl->setup) && is_array($GLOBALS['TSFE']->tmpl->setup)) {
72 93
                    $configurationArray = $GLOBALS['TSFE']->tmpl->setup;
73 93
                    $this->typoScriptConfigurations['default'] = $configurationArray;
74
                }
75
            }
76
        }
77
78 235
        if (!is_array($configurationArray)) {
79 79
            $configurationArray = [];
80
        }
81
82 235
        if (!isset($configurationArray['plugin.']['tx_solr.'])) {
83 96
            $configurationArray['plugin.']['tx_solr.'] = [];
84
        }
85
86 235
        if ($contextPageId === null && !empty($GLOBALS['TSFE']->id)) {
87 92
            $contextPageId = $GLOBALS['TSFE']->id;
88
        }
89
90 235
        $hash = md5(serialize($configurationArray)) . '-' . $contextPageId . '-' . $contextLanguageId . '-' . $contextTypoScriptPath;
91 235
        if (isset($this->typoScriptConfigurations[$hash])) {
92 153
            return $this->typoScriptConfigurations[$hash];
93
        }
94
95 174
        $this->typoScriptConfigurations[$hash] = $this->getTypoScriptConfigurationInstance($configurationArray, $contextPageId);
96 174
        return $this->typoScriptConfigurations[$hash];
97
    }
98
99
    /**
100
     * This method is used to build the TypoScriptConfiguration.
101
     *
102
     * @param array $configurationArray
103
     * @param int|null $contextPageId
104
     * @return object
105
     */
106 174
    protected function getTypoScriptConfigurationInstance(array $configurationArray = null, $contextPageId = null)
107
    {
108 174
        return GeneralUtility::makeInstance(
109
            TypoScriptConfiguration::class,
110
            /** @scrutinizer ignore-type */ $configurationArray,
111
            /** @scrutinizer ignore-type */ $contextPageId
112
        );
113
    }
114
}
115