Passed
Pull Request — develop (#167)
by
unknown
20:25
created

ConfigurationContainer::getValueByPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Codappix\SearchCore\Configuration;
4
5
/*
6
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
 * 02110-1301, USA.
22
 */
23
24
use TYPO3\CMS\Core\Utility\ArrayUtility;
25
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
26
27
/**
28
 * Container of all configurations for extension.
29
 * Always inject this to have a single place for configuration and parsing only once.
30
 */
31
class ConfigurationContainer implements ConfigurationContainerInterface
32
{
33
    /**
34
     * Plain TypoScript array from extbase for extension / plugin.
35
     *
36
     * @var array
37
     */
38
    protected $settings = [];
39
40
    /**
41
     * Inject settings via ConfigurationManager.
42
     *
43
     * @param ConfigurationManagerInterface $configurationManager
44 62
     */
45
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
46 62
    {
47 62
        $this->settings = $configurationManager->getConfiguration(
48 62
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
49 62
            'SearchCore',
50
            'Results'
51 62
        );
52
        if ($this->settings === null) {
53
            throw new NoConfigurationException('Could not fetch configuration.', 1484226842);
54 62
        }
55
    }
56
57
    /**
58
     * @param string $path In dot notation.
59
     * @return mixed
60
     * @throws InvalidArgumentException
61 54
     */
62
    public function get(string $path)
63 54
    {
64
        $value = $this->getValueByPath($path);
65 54
66 32
        if ($value === null) {
67 32
            throw new InvalidArgumentException(
68 32
                'The given configuration option "' . $path . '" does not exist.',
69
                InvalidArgumentException::OPTION_DOES_NOT_EXIST
70
            );
71
        }
72 50
73
        return $value;
74
    }
75
76
    /**
77
     * @param string $path In dot notation.
78
     * @return mixed
79 26
     */
80
    public function getIfExists(string $path)
81 26
    {
82
        return $this->getValueByPath($path);
83
    }
84
85
    /**
86
     * @param string $path
87
     * @return mixed
88
     */
89
    protected function getValueByPath(string $path)
90
    {
91
        try {
92
            return ArrayUtility::getValueByPath($this->settings, $path, '.');
93
        } catch (\Exception $e) {
94
            // Catch all exceptions to safely handle path retrieval
95
        }
96
        return null;
97
    }
98
}
99