EnvFileHelper::set()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 4
dl 0
loc 31
rs 9.6666
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 .= "\n$comments";
50
            }
51
            $content .= "\n$key=value";
52
            /*if ($comment) {
53
                $content .= <<<ENVVAR
54
$comments
55
ENVVAR;
56
            }
57
            $content .= <<<ENVVAR
58
$key=$value
59
ENVVAR;*/
60
        }
61
        $fileSystem = new Filesystem();
62
        $fileSystem->dumpFile($this->filePath, $content ?? '');
63
        if ($setOwnership) {
64
            $dirInfo = new \SplFileInfo(\dirname($this->filePath));
65
            chown($this->filePath, $dirInfo->getOwner());
66
            chgrp($this->filePath, $dirInfo->getGroup());
67
        }
68
    }
69
70
    /**
71
     * @param string $envName
72
     * @return bool
73
     * @throws PcreException
74
     * @throws FilesystemException
75
     */
76
    private function has(string $envName): bool
77
    {
78
        $content = $this->getContent();
79
        return (bool) preg_match("/^$envName=/m", $content);
80
    }
81
82
    /**
83
     * @return string
84
     * @throws FilesystemException
85
     */
86
    private function getContent(): string
87
    {
88
        if (!\file_exists($this->filePath)) {
89
            return '';
90
        }
91
        $content = file_get_contents($this->filePath);
92
        return $content;
93
    }
94
}
95