ContextualConfiguration::getForContext()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.3357

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 15
cts 19
cp 0.7895
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 8
nop 4
crap 6.3357
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Config;
14
15
use Closure;
16
use Rocketeer\Services\Connections\Credentials\Keys\ConnectionKey;
17
use Rocketeer\Services\Container\Container;
18
use Rocketeer\Traits\ContainerAwareTrait;
19
20
/**
21
 * Gets configuration relative to the current connection/plugin.
22
 *
23
 * @mixin Configuration
24
 */
25
class ContextualConfiguration implements ConfigurationInterface
26
{
27
    use ContainerAwareTrait;
28
29
    /**
30
     * @var Configuration
31
     */
32
    private $configuration;
33
34
    /**
35
     * @param Container     $container
36
     * @param Configuration $configuration
37
     */
38 442
    public function __construct(Container $container, Configuration $configuration)
39
    {
40 442
        $this->container = $container;
41 442
        $this->configuration = $configuration;
42 442
    }
43
44
    ////////////////////////////////////////////////////////////////////////////////
45
    ////////////////////////////// BASE CONFIGURATION //////////////////////////////
46
    ////////////////////////////////////////////////////////////////////////////////
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 442
    public function get($key, $default = null)
52
    {
53 442
        return $this->configuration->get($key, $default);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 442
    public function set($key, $value)
60
    {
61 442
        return $this->configuration->set($key, $value);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 442
    public function replace(array $items)
68
    {
69 442
        return $this->configuration->replace($items);
70
    }
71
72
    /**
73
     * @return array
74
     */
75 4
    public function all()
76
    {
77 4
        return $this->configuration->all();
78
    }
79
80
    ////////////////////////////////////////////////////////////////////////////////
81
    //////////////////////////// CONTEXTUAL CONFIGURATION //////////////////////////
82
    ////////////////////////////////////////////////////////////////////////////////
83
84
    /**
85
     * Get an option for a plugin.
86
     *
87
     * @param string      $plugin
88
     * @param string|null $option
89
     *
90
     * @return mixed
91
     */
92 1
    public function getPluginOption($plugin, $option = null)
93
    {
94 1
        $option = $option ? '.'.$option : '';
95
96 1
        return $this->config->get('plugins.config.'.$plugin.$option);
97
    }
98
99
    /**
100
     * Get an option from Rocketeer's config file.
101
     *
102
     * @param string             $option
103
     * @param ConnectionKey|null $connectionKey
104
     *
105
     * @return array|Closure|string
106
     */
107 442
    public function getContextually($option, ConnectionKey $connectionKey = null)
108
    {
109 442
        $original = $this->configuration->get($option);
110 442
        $connectionKey = $connectionKey ?: $this->connections->getCurrentConnectionKey();
111
112 442
        $contexts = ['stages', 'connections', 'servers'];
113 442
        foreach ($contexts as $context) {
114 442
            if ($contextual = $this->getForContext($option, $context, $original, $connectionKey)) {
115 6
                return $contextual;
116
            }
117 442
        }
118
119 442
        return $original;
120
    }
121
122
    /**
123
     * Get a contextual option.
124
     *
125
     * @param string             $option
126
     * @param string             $type
127
     * @param string|array|null  $original
128
     * @param ConnectionKey|null $connectionKey
129
     *
130
     * @return array|Closure|string
131
     */
132 442
    protected function getForContext($option, $type, $original, ConnectionKey $connectionKey)
133
    {
134
        // Switch context
135
        switch ($type) {
136 442
            case 'servers':
137 442
                $contextual = sprintf('connections.%s.servers.%d.config.%s', $connectionKey->name, $connectionKey->server, $option);
138 442
                break;
139
140 442
            case 'stages':
141 442
                $contextual = sprintf('on.stages.%s.%s', $connectionKey->stage, $option);
142 442
                break;
143
144 442
            case 'connections':
145 442
                $contextual = sprintf('on.connections.%s.%s', $connectionKey->name, $option);
146 442
                break;
147
148
            default:
149
                $contextual = $option;
150
                break;
151
        }
152
153
        // Merge with defaults
154 442
        $value = $this->configuration->get($contextual);
155 442
        if (is_array($value) && $original) {
156 2
            $value = array_replace($original, $value);
157 2
        }
158
159 442
        return $value;
160
    }
161
}
162