Passed
Pull Request — develop (#167)
by Daniel
04:49
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 Codappix\SearchCore\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.
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
     * @throws NoConfigurationException
44
     */
45 64
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
46
    {
47 64
        $this->settings = $configurationManager->getConfiguration(
48 64
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
49 64
            'SearchCore'
50
        );
51 64
        if ($this->settings === null) {
52
            throw new NoConfigurationException('Could not fetch configuration.', 1484226842);
53
        }
54 64
    }
55
56
    /**
57
     * @param string $path In dot notation.
58
     * @return mixed
59
     * @throws InvalidArgumentException
60
     */
61 56
    public function get(string $path)
62
    {
63 56
        $value = $this->getValueByPath($path);
64
65 56
        if ($value === null) {
66 34
            throw new InvalidArgumentException(
67 34
                'The given configuration option "' . $path . '" does not exist.',
68 34
                InvalidArgumentException::OPTION_DOES_NOT_EXIST
69
            );
70
        }
71
72 52
        return $value;
73
    }
74
75
    /**
76
     * @param string $path In dot notation.
77
     * @return mixed
78
     */
79 28
    public function getIfExists(string $path)
80
    {
81 28
        return $this->getValueByPath($path);
82
    }
83
84
    /**
85
     * @param string $path In dot notation.
86
     * @return mixed|null Null if no entry was found.
87
     */
88 56
    protected function getValueByPath(string $path)
89
    {
90
        try {
91 56
            return ArrayUtility::getValueByPath($this->settings, $path, '.');
92 42
        } catch (\Exception $e) {
93
            // Catch all exceptions to safely handle path retrieval
94
        }
95 42
        return null;
96
    }
97
}
98