Passed
Pull Request — master (#40)
by Chema
03:06
created

EnvConfigReader::read()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
crap 4.0961
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\ConfigReader;
6
7
use Gacela\Framework\Config\ConfigReaderInterface;
8
9
final class EnvConfigReader implements ConfigReaderInterface
10
{
11 2
    public function canRead(string $absolutePath): bool
12
    {
13 2
        return false !== strpos($absolutePath, '.env');
14
    }
15
16 2
    public function read(string $absolutePath): array
17
    {
18 2
        if (!is_file($absolutePath)) {
19
            return [];
20
        }
21
22 2
        $config = [];
23 2
        $lines = file($absolutePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
24
25 2
        foreach ($lines as $line) {
26 2
            if (strpos(trim($line), '#') === 0) {
27
                continue;
28
            }
29
30 2
            [$name, $value] = explode('=', $line, 2);
31 2
            $config[trim($name)] = trim($value);
32
        }
33
34 2
        return $config;
35
    }
36
}
37