DotEnvV2FileReaderAdapter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\MultiEnv\FileReader;
6
7
use Lamoda\MultiEnv\FileReader\Exception\EnvFileReaderException;
8
use Lamoda\MultiEnv\FileReader\FileNameResolver\FileNameResolverInterface;
9
use Lamoda\MultiEnv\FileReader\PathResolver\PathResolverInterface;
10
use Lamoda\MultiEnv\Model\HostId;
11
12
final class DotEnvV2FileReaderAdapter implements EnvFileReaderInterface
13
{
14
    /**
15
     * @var PathResolverInterface
16
     */
17
    private $pathResolver;
18
19
    /**
20
     * @var FileNameResolverInterface
21
     */
22
    private $fileNameResolver;
23
24
    /**
25
     * @var bool
26
     */
27
    private $isEnvFileAlreadyLoaded;
28
29 9
    public function __construct(PathResolverInterface $pathResolver, FileNameResolverInterface $fileNameResolver)
30
    {
31
        // @codeCoverageIgnoreStart
32
        if (!class_exists(\Dotenv\Dotenv::class)) {
33
            throw EnvFileReaderException::becauseAdapterCanNotBeCreated(__CLASS__, \Dotenv\Dotenv::class);
34
        }
35
        // @codeCoverageIgnoreEnd
36 9
        $this->pathResolver = $pathResolver;
37 9
        $this->fileNameResolver = $fileNameResolver;
38 9
        $this->isEnvFileAlreadyLoaded = false;
39 9
    }
40
41 15
    public function readEnvFile(HostId $hostId): void
42
    {
43 15
        if ($this->isEnvFileAlreadyLoaded) {
44 1
            return;
45
        }
46
47
        try {
48 15
            $dotEnv = new \Dotenv\Dotenv(
49 15
                $this->pathResolver->resolvePathToEnvFile($hostId),
50 15
                $this->fileNameResolver->resolveEnvFileName($hostId)
51
            );
52 15
            $dotEnv->overload();
53 14
            $this->isEnvFileAlreadyLoaded = true;
54 1
        } catch (\Dotenv\Exception\ExceptionInterface $exception) {
55 1
            throw EnvFileReaderException::becauseEnvFileCanNotBeProcessed($exception);
56
        }
57 14
    }
58
}
59