Completed
Push — master ( 86d06a...811f1b )
by TJ
02:37
created

EnvFile   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 60
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 6 1
A isEmpty() 0 4 1
A toArray() 0 4 1
A close() 0 4 1
A linesFromFile() 0 7 1
A convertLineValuesToArray() 0 6 1
A mapLineValuesAndKeys() 0 10 2
1
<?php
2
3
namespace sixlive\DotenvEditor;
4
5
use SplFileObject;
6
use sixlive\DotenvEditor\Support\Arr;
7
8
class EnvFile
9
{
10
    /**
11
     * @var \SplFileObject|null
12
     */
13
    protected $file;
14
15 8
    public function __construct($path)
16
    {
17 8
        $this->file = new SplFileObject($path, 'r+');
18 8
    }
19
20 6
    public function write($content)
21
    {
22 6
        $this->file->fwrite($content);
23
24 6
        return $this;
25
    }
26
27 8
    public function isEmpty()
28
    {
29 8
        return $this->file->getSize() > 0;
30
    }
31
32 3
    public function toArray()
33
    {
34 3
        return Arr::flatten($this->mapLineValuesAndKeys());
35
    }
36
37 8
    public function close()
38
    {
39 8
        $this->file = null;
40 8
    }
41
42 3
    private function linesFromFile()
43
    {
44 3
        return explode(
45 3
            "\n",
46 3
            $this->file->fread($this->file->getSize())
47
        );
48
    }
49
50
    private function convertLineValuesToArray()
51
    {
52 3
        return array_map(function ($line) {
53 3
            return explode('=', $line);
54 3
        }, $this->linesFromFile());
55
    }
56
57
    private function mapLineValuesAndKeys()
58
    {
59 3
        return array_map(function ($line) {
60 3
            if (count($line) === 2) {
61 3
                return [$line[0] => $line[1]];
62
            }
63
64 3
            return $line[0];
65 3
        }, $this->convertLineValuesToArray());
66
    }
67
}
68