Completed
Push — master ( 6f58e5...e4420b )
by Petre
03:34
created

ConsulEnvManager::exposeEnvironmentIntoContainer()   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
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace DL\ConsulPhpEnvVar\Service;
5
6
use SensioLabs\Consul\Services\KV;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * Manages environment variables through Consul.
11
 *
12
 * @package DL\ConsulPhpEnvVar\Service
13
 * @author  Petre Pătrașc <[email protected]>
14
 */
15
class ConsulEnvManager
16
{
17
    /**
18
     * @var KV
19
     */
20
    protected $kv;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $overwriteEvenIfDefined = false;
26
27
    /**
28
     * ConsulEnvManager constructor.
29
     *
30
     * @param KV   $kv
31
     * @param bool $overwriteEvenIfDefined
32
     */
33 8
    public function __construct(KV $kv, bool $overwriteEvenIfDefined = false)
34
    {
35 8
        $this->kv                     = $kv;
36 8
        $this->overwriteEvenIfDefined = $overwriteEvenIfDefined;
37 8
    }
38
39
    /**
40
     * Add missing environment variables from Consul.
41
     *
42
     * @param array $mappings
43
     */
44 3
    public function getEnvVarsFromConsul(array $mappings)
45
    {
46 3
        foreach ($mappings as $environmentKey => $consulPath) {
47 3
            $keyExists = $this->keyIsDefined($environmentKey);
48 3
            if ($keyExists && !$this->overwriteEvenIfDefined) {
49 1
                continue;
50
            }
51
52 2
            $consulValue = $this->getKeyValueFromConsul($consulPath);
53 2
            $this->saveKeyValueInEnvironmentVars($environmentKey, $consulValue);
54
        }
55 3
    }
56
57
    /**
58
     * Expose a set of mappings into the container builder.
59
     *
60
     * @param ContainerBuilder $container
61
     * @param array            $mappings
62
     *
63
     * @return ContainerBuilder
64
     */
65 1
    public function exposeEnvironmentIntoContainer(ContainerBuilder $container, array $mappings): ContainerBuilder
66
    {
67 1
        foreach ($mappings as $environmentKey => $consulPath) {
68 1
            $container->setParameter("env({$environmentKey})", getenv($environmentKey));
69
        }
70
71 1
        return $container;
72
    }
73
74
    /**
75
     * Check if an environment key is defined.
76
     *
77
     * @param string $environmentKey
78
     *
79
     * @return bool
80
     */
81 3
    private function keyIsDefined(string $environmentKey): bool
82
    {
83 3
        $keyValue = getenv($environmentKey);
84
85 3
        if (false === $keyValue) {
86 1
            return false;
87
        }
88
89 2
        return true;
90
    }
91
92
    /**
93
     * Get a key value from Consul.
94
     *
95
     * @param string $kvPath
96
     *
97
     * @return string
98
     */
99 2
    private function getKeyValueFromConsul(string $kvPath): string
100
    {
101 2
        return $this->kv->get($kvPath, ['raw' => true])->getBody();
102
    }
103
104
    /**
105
     * Save the value of a key in an environment variable.
106
     *
107
     * @param string $envKey
108
     * @param string $kvValue
109
     */
110 2
    private function saveKeyValueInEnvironmentVars($envKey, $kvValue)
111
    {
112 2
        putenv("{$envKey}={$kvValue}");
113 2
    }
114
}
115