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

EnvConfigReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canRead() 0 3 1
A read() 0 19 4
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