Completed
Push — master ( 7683eb...d4c560 )
by Julien
07:10 queued 04:36
created

EnvFileHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Helper;
4
5
use Safe\Exceptions\FilesystemException;
6
use Safe\Exceptions\PcreException;
7
use Symfony\Component\Filesystem\Filesystem;
8
use function \Safe\chown;
9
use function \Safe\chgrp;
10
use function \Safe\preg_match;
11
use function \Safe\file_get_contents;
12
13
final class EnvFileHelper
14
{
15
    /** @var string */
16
    private $filePath;
17
18
    /**
19
     * EnvFileHelper constructor.
20
     * @param string $filePath
21
     */
22
    public function __construct(string $filePath)
23
    {
24
        $this->filePath = $filePath;
25
    }
26
27
    /**
28
     * Adds or updates an environment variable.
29
     * @param string $key
30
     * @param string $value
31
     * @param string|null $comment
32
     * @param bool $setOwnership
33
     * @throws PcreException
34
     * @throws FilesystemException
35
     */
36
    public function set(string $key, string $value, string $comment = null, bool $setOwnership = true): void
37
    {
38
        $content = $this->getContent();
39
        if ($this->has($key)) {
40
            // Note: if the key is already in the file, comments are not modified.
41
            $content = \preg_replace("/^$key=.*/m", $key.'='.$value, $content);
42
        } else {
43
            $commentLines = \explode("\n", $comment ?? '');
44
            $commentLines = \array_map(function (string $line) {
45
                return '# '.$line;
46
            }, $commentLines);
47
            $comments = \implode("\n", $commentLines);
48
            if ($comment) {
49
                $content .= <<<ENVVAR
50
$comments
51
ENVVAR;
52
            }
53
            $content .= <<<ENVVAR
54
$key=$value
55
ENVVAR;
56
        }
57
        $fileSystem = new Filesystem();
58
        $fileSystem->dumpFile($this->filePath, $content ?? '');
59
        if ($setOwnership) {
60
            $dirInfo = new \SplFileInfo(\dirname($this->filePath));
61
            chown($this->filePath, $dirInfo->getOwner());
62
            chgrp($this->filePath, $dirInfo->getGroup());
63
        }
64
    }
65
66
    /**
67
     * @param string $envName
68
     * @return bool
69
     * @throws PcreException
70
     * @throws FilesystemException
71
     */
72
    private function has(string $envName): bool
73
    {
74
        $content = $this->getContent();
75
        return (bool) preg_match("/^$envName=/m", $content);
76
    }
77
78
    /**
79
     * @return string
80
     * @throws FilesystemException
81
     */
82
    private function getContent(): string
83
    {
84
        if (!\file_exists($this->filePath)) {
85
            return '';
86
        }
87
        $content = file_get_contents($this->filePath);
88
        return $content;
89
    }
90
}
91