Completed
Pull Request — develop (#167)
by
unknown
05:23
created

ConfigurationContainer::getValueByPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 64
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
44
    {
45 64
        $this->settings = $configurationManager->getConfiguration(
46 64
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
47 64
            'SearchCore',
48 64
            'Search'
49
        );
50 64
        if ($this->settings === null) {
51
            throw new NoConfigurationException('Could not fetch configuration.', 1484226842);
52
        }
53 64
    }
54
55
    /**
56
     * @param string $path In dot notation.
57
     * @return mixed
58
     * @throws InvalidArgumentException
59
     */
60 56
    public function get(string $path)
61
    {
62 56
        $value = $this->getValueByPath($path);
63
64 56
        if ($value === null) {
65 34
            throw new InvalidArgumentException(
66 34
                'The given configuration option "' . $path . '" does not exist.',
67 34
                InvalidArgumentException::OPTION_DOES_NOT_EXIST
68
            );
69
        }
70
71 52
        return $value;
72
    }
73
74
    /**
75
     * @param string $path In dot notation.
76
     * @return mixed
77
     */
78 28
    public function getIfExists(string $path)
79
    {
80 28
        return $this->getValueByPath($path);
81
    }
82
83
    /**
84
     * @param string $path In dot notation.
85
     * @return mixed|null Null if no entry was found.
86
     */
87 56
    protected function getValueByPath(string $path)
88
    {
89
        try {
90 56
            return ArrayUtility::getValueByPath($this->settings, $path, '.');
91 42
        } catch (\Exception $e) {
92
            // Catch all exceptions to safely handle path retrieval
93
        }
94 42
        return null;
95
    }
96
}
97