ConfigurationAdapter::getOptions()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 0
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
4
namespace Bencagri\Artisan\Deployer\Configuration;
5
6
use Bencagri\Artisan\Deployer\Helper\Str;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
9
10
final class ConfigurationAdapter
11
{
12
    private $config;
13
    /** @var ParameterBag */
14
    private $options;
15
16
    public function __construct(AbstractConfiguration $config)
17
    {
18
        $this->config = $config;
19
    }
20
21
    public function __toString() : string
22
    {
23
        return Str::formatAsTable($this->getOptions()->all());
24
    }
25
26
    public function get(string $optionName)
27
    {
28
        if (!$this->getOptions()->has($optionName)) {
29
            throw new \InvalidArgumentException(sprintf('The "%s" option is not defined.', $optionName));
30
        }
31
32
        return $this->getOptions()->get($optionName);
33
    }
34
35
    private function getOptions() : ParameterBag
36
    {
37
        if (null !== $this->options) {
38
            return $this->options;
39
        }
40
41
        // it's not the most beautiful code possible, but making the properties
42
        // private and the methods public allows to configure the deployment using
43
        // a config builder and the IDE autocompletion. Here we need to access
44
        // those private properties and their values
45
        $options = new ParameterBag();
46
        $r = new \ReflectionObject($this->config);
47
        foreach ($r->getProperties() as $property) {
48
            try {
49
                $property->setAccessible(true);
50
                $options->set($property->getName(), $property->getValue($this->config));
51
            } catch (\ReflectionException $e) {
52
                // ignore this error
53
            }
54
        }
55
56
        return $this->options = $options;
57
    }
58
}
59