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

EnvConfigReader::canRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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