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

EnvFile::configLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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