Completed
Push — master ( c4e506...aa3235 )
by TJ
01:52 queued 17s
created

DotenvEditor::save()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace sixlive\DotenvEditor;
4
5
use sixlive\DotenvEditor\Support\Arr;
6
7
class DotenvEditor
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $env = [];
13
14
    /**
15
     * @var \sixlive\DotenvEditor\EnvFile
16
     */
17
    protected $envFile;
18
19
    /**
20
     * Load an values from an env file.
21
     *
22
     * @param  string  $path
23
     *
24
     * @return self
25
     */
26 9
    public function load($path)
27
    {
28 9
        $this->envFile = new EnvFile($path);
29
30 9
        if ($this->envFile->isEmpty()) {
31 3
            $this->env = array_merge($this->env, $this->envFile->toArray());
32
        }
33
34 9
        return $this;
35
    }
36
37
    /**
38
     * Set a key value pair for the env file.
39
     *
40
     * @param  string  $key
41
     * @param  string  $value
42
     *
43
     * @return self
44
     */
45 9
    public function set($key, $value)
46
    {
47 9
        $this->env[$key] = $value;
48
49 9
        return $this;
50
    }
51
52
    /**
53
     * Get all of the env values or a single value by key.
54
     *
55
     * @param  string  $key
56
     *
57
     * @return array|string
58
     */
59 3
    public function getEnv($key = '')
60
    {
61 3
        return isset($this->env[$key])
62 1
            ? $this->env[$key]
63 3
            : $this->env;
64
    }
65
66
    /**
67
     * Save the current representation to disk. If no path is specifed and
68
     * a file was loaded, it will overwrite the file that was loaded.
69
     *
70
     * @param  string  $path
71
     *
72
     * @return self
73
     */
74 7
    public function save($path = '')
75
    {
76 7
        if (empty($path) && $this->envFile) {
77 6
            $this->envFile->write($this->format());
78
        } else {
79 1
            file_put_contents($path, $this->format());
80
        }
81
82 7
        return $this;
83
    }
84
85
    /**
86
     * Add an empty line to the config.
87
     *
88
     * @return self
89
     */
90 2
    public function addEmptyLine()
91
    {
92 2
        $this->env[] = '';
93
94 2
        return $this;
95
    }
96
97
    /**
98
     * Add a comment heading. If there is a line before it, it will add an empty
99
     * line before the heading.
100
     *
101
     * @param  string  $heading
102
     *
103
     * @return self
104
     */
105 3
    public function heading($heading)
106
    {
107 3
        if (! empty(end($this->env))) {
108 1
            $this->addEmptyLine();
109
        }
110
111 3
        $this->env[] = sprintf('# %s', $heading);
112
113 3
        return $this;
114
    }
115
116
    /**
117
     * Check if a key is defined in the env.
118
     *
119
     * @param  string  $key
120
     *
121
     * @return bool
122
     */
123 1
    public function has($key)
124
    {
125 1
        return isset($this->env[$key]);
126
    }
127
128 11
    public function __destruct()
129
    {
130 11
        if ($this->envFile) {
131 9
            $this->envFile->close();
132
        }
133 11
    }
134
135
    /**
136
     * Format the config file in key=value pairs.
137
     *
138
     * @return string
139
     */
140
    private function format()
141
    {
142 7
        $valuePairs = Arr::mapWithKeys($this->env, function ($item, $key) {
143 7
            return ! empty($item) && ! is_int($key)
144 7
                ? sprintf('%s=%s', $key, $item)
145 7
                : $item;
146 7
        });
147
148 7
        return implode("\n", $valuePairs);
149
    }
150
}
151