Completed
Push — master ( ffd18f...78e9e4 )
by Michael
04:35
created

ComposerExtra::getConfig()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 14
nc 7
nop 0
1
<?php
2
3
namespace Schnittstabil\ComposerExtra;
4
5
use function Schnittstabil\Get\getValue;
6
use function Schnittstabil\Get\getValueOrFail;
7
use Schnittstabil\Get\Get;
8
9
/**
10
 * Get namespaced configuration from `composer.json`.
11
 */
12
class ComposerExtra
13
{
14
    use PresetsAwareTrait;
15
    use ComposerJsonAwareTrait;
16
17
    protected $config;
18
    protected $namespace;
19
    protected $defaultConfig;
20
    protected $presetsPath;
21
22
    /**
23
     * Merge configurations.
24
     *
25
     * @var callable
26
     */
27
    public $merge = '\Schnittstabil\ConfigMerge\config_merge';
28
29
    /**
30
     * Create a new ComposerExtra.
31
     *
32
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`.
33
     *
34
     * @param string|int|mixed[] $namespace     a `Schnittstabil\Get\getValue` path
35
     * @param mixed              $defaultConfig default configuration
36
     * @param string             $presetsPath   presets path (w/o namespace)
37
     *
38
     * @SuppressWarnings(PHPMD.StaticAccess)
39
     */
40
    public function __construct($namespace = array(), $defaultConfig = null, $presetsPath = null)
41
    {
42
        $this->namespace = Get::normalizePath($namespace);
43
        array_unshift($this->namespace, 'extra');
44
        $this->defaultConfig = $defaultConfig === null ? new \stdClass() : $defaultConfig;
45
        $this->presetsPath = $presetsPath;
46
    }
47
48
    /**
49
     * Get the configuration.
50
     *
51
     * @return mixed the configuration
52
     */
53
    protected function getConfig()
54
    {
55
        if ($this->config !== null) {
56
            return $this->config;
57
        }
58
59
        $config = $this->defaultConfig;
60
        $composerConfig = getValue($this->namespace, $this->loadComposerJson());
61
62
        if ($composerConfig !== null) {
63
            $config = call_user_func($this->merge, $config, $composerConfig);
64
        }
65
66
        if ($this->presetsPath === null || $config === null) {
67
            return $this->config = $config;
68
        }
69
70
        $presets = $this->loadPresets(getValue($this->presetsPath, $config, []));
71
72
        if (is_array($presets) && count($presets) > 0) {
73
            $presetsConfig = array_reduce($presets, $this->merge, array_shift($presets));
74
            $config = call_user_func($this->merge, $presetsConfig, $config);
75
        }
76
77
        return $this->config = $config;
78
    }
79
80
    /**
81
     * Get configuration value.
82
     *
83
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`.
84
     *
85
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValue` path
86
     * @param mixed              $default default value if $path is not valid
87
     *
88
     * @return mixed the value determined by `$path` or otherwise `$default`
89
     */
90
    public function get($path = array(), $default = null)
91
    {
92
        return getValue($path, $this->getConfig(), $default);
93
    }
94
95
    /**
96
     * Get configuration value.
97
     *
98
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValueOrFail`.
99
     *
100
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValueOrFail` path
101
     * @param mixed              $message exception message
102
     *
103
     * @throws \OutOfBoundsException if `$path` is not valid
104
     *
105
     * @return mixed the value determined by `$path`
106
     */
107
    public function getOrFail($path = array(), $message = null)
108
    {
109
        return getValueOrFail($path, $this->getConfig(), $message);
110
    }
111
}
112