getTypoScriptConfigurationArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Asdis\System\Configuration;
3
4
use Aoe\Asdis\System\Configuration\Exception\InvalidTypoScriptSetting;
5
use Aoe\Asdis\System\Configuration\Exception\TypoScriptSettingNotExists;
6
use TYPO3\CMS\Core\SingletonInterface;
7
use TYPO3\CMS\Core\TypoScript\TemplateService;
8
use TYPO3\CMS\Core\Utility\GeneralUtility;
9
use TYPO3\CMS\Core\Utility\RootlineUtility;
10
11
/**
12
 * TypoScript configuration provider.
13
 */
14
class TypoScriptConfiguration implements SingletonInterface 
15
{
16
    /**
17
     * @var array
18
     */
19
    private $configuration;
0 ignored issues
show
introduced by
The private property $configuration is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var array
23
     */
24
    private $configurationCache = [];
25
26
    /**
27
     * @param string $key The setting key. E.g. "logger.severity"
28
     * @param string $validateType The data type to be validated against (E.g. "string"). Empty string to skip validation.
29
     * @param boolean $hasSubkeys Tells whether the requested key is assumed to has subkeys.
30
     * @return mixed
31
     * @throws \Aoe\Asdis\System\Configuration\Exception\InvalidTypoScriptSetting
32
     * @throws \Aoe\Asdis\System\Configuration\Exception\TypoScriptSettingNotExists
33
     */
34 2
    public function getSetting($key, $validateType = '', $hasSubkeys = false)
35
    {
36 2
        if (isset($this->configurationCache[$key])) {
37
            return $this->configurationCache[$key];
38
        }
39 2
        $parts = explode('.', $key);
40 2
        if (false === is_array($parts) || sizeof($parts) < 1) {
41
            throw new TypoScriptSettingNotExists($key, 1372050700894);
42
        }
43 2
        $conf = $this->getTypoScriptConfigurationArray();
44 2
        $lastPartIndex = sizeof($parts) - 1;
45 2
        foreach($parts as $index => $part) {
46 2
            $subkey = $part;
47 2
            if ($lastPartIndex !== $index || $hasSubkeys) {
48 1
                $subkey .= '.';
49
            }
50 2
            if (false === isset($conf[$subkey])) {
51 1
                throw new TypoScriptSettingNotExists($key, 1372063884313);
52
            }
53 1
            $conf = $conf[$subkey];
54 1
            if ($lastPartIndex === $index) {
55 1
                break;
56
            }
57
        }
58 1
        if (strlen($validateType) > 0 && strcmp($validateType, gettype($conf)) !== 0) {
59 1
            throw new InvalidTypoScriptSetting($key, gettype($conf), 1372064668444);
60
        }
61
        $this->configurationCache[$key] = $conf;
62
        return $conf;
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    protected function getTypoScriptConfigurationArray()
69
    {
70
        if (true === version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '9.5.0', '<')) {
71
            return $GLOBALS['TSFE']->tmpl->setup['config.']['tx_asdis.'];
72
        }
73
74
        /** @var Site $site */
75
        $site = $GLOBALS['TYPO3_REQUEST']->getAttribute('site');
76
        /** @var RootlineUtility $rootlineUtility */
77
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $site->getRootPageId());
78
        $rootline = $rootlineUtility->get();
79
        /** @var TemplateService $templateService */
80
        $templateService = GeneralUtility::makeInstance(TemplateService::class);
81
        $templateService->tt_track = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $tt_track was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
82
        $templateService->runThroughTemplates($rootline);
83
        $templateService->generateConfig();
84
85
        return $templateService->setup['config.']['tx_asdis.'];
86
    }
87
}