Completed
Push — master ( 702208...95f574 )
by TJ
02:28
created

DotenvEditor   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 144
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 2
A set() 0 6 1
A getEnv() 0 6 2
A save() 0 10 3
A addEmptyLine() 0 6 1
A heading() 0 10 2
A has() 0 4 1
A __destruct() 0 6 2
A format() 0 10 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 8
    public function load($path)
27
    {
28 8
        $this->envFile = new EnvFile($path);
29
30 8
        if ($this->envFile->isEmpty()) {
31 3
            $this->env = array_merge($this->env, $this->envFile->toArray());
32
        }
33
34 8
        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 8
    public function set($key, $value)
46
    {
47 8
        $this->env[$key] = $value;
48
49 8
        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 6
    public function save($path = '')
75
    {
76 6
        if (empty($path) && $this->envFile) {
77 6
            $this->envFile->write($this->format());
78
        } else {
79
            file_put_contents($path, $this->format());
80
        }
81
82 6
        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 boolean
122
     */
123 1
    public function has($key)
124
    {
125 1
        return isset($this->env[$key]);
126
    }
127
128 10
    public function __destruct()
129
    {
130 10
        if ($this->envFile) {
131 8
            $this->envFile->close();
132
        }
133 10
    }
134
135
    /**
136
     * Format the config file in key=value pairs.
137
     *
138
     * @return string
139
     */
140
    private function format()
141
    {
142 6
        $valuePairs = Arr::mapWithKeys($this->env, function ($item, $key) {
143 6
            return ! empty($item) && ! is_int($key)
144 6
                ? sprintf('%s=%s', $key, $item)
145 6
                : $item;
146 6
        });
147
148 6
        return implode("\n", $valuePairs);
149
    }
150
}
151