EnvFile::marshallIntoEnvironment()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPEnv\Marshallers;
6
7
use RemotelyLiving\PHPEnv\Exceptions;
8
use RemotelyLiving\PHPEnv\Interfaces;
9
10
final class EnvFile implements Interfaces\Marshaller
11
{
12
    private \SplFileObject $envFile;
13
14
    private function __construct(\SplFileObject $envFile)
15
    {
16
        $this->envFile = $envFile;
17
    }
18
19
    public function marshallIntoEnvironment(): void
20
    {
21
        $this->envFile->rewind();
22
        ;
23
        while ($line = $this->envFile->fgets()) {
24
            $trimmed = trim($line);
25
            if (!$trimmed) {
26
                continue;
27
            }
28
29
            \putenv($trimmed);
30
        }
31
    }
32
33
    public static function create(\SplFileObject $envFile): self
34
    {
35
        return new self($envFile);
36
    }
37
38
    public static function createFromPath(string $filePath): self
39
    {
40
        try {
41
            $envFile = new \SplFileObject($filePath, 'r');
42
        } catch (\Throwable $e) {
43
            throw Exceptions\RuntimeError::envFileNotFound($filePath);
44
        }
45
46
        return self::create($envFile);
47
    }
48
}
49