ConfigurationAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getOptions() 0 22 4
A __construct() 0 3 1
A __toString() 0 3 1
A get() 0 7 2
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