FileNameResolver::resolveEnvFileName()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\MultiEnv\FileReader\FileNameResolver;
6
7
use Lamoda\MultiEnv\Formatter\FormatterInterface;
8
use Lamoda\MultiEnv\Model\HostId;
9
10
final class FileNameResolver implements FileNameResolverInterface
11
{
12
    public const DEFAULT_FILE_NAME = '.env';
13
    /**
14
     * @var string
15
     */
16
    private $originalFileName;
17
18
    /**
19
     *@var FormatterInterface|null
20
     */
21
    private $formatter;
22
23 15
    public function __construct(string $originalFileName = self::DEFAULT_FILE_NAME, FormatterInterface $formatter = null)
24
    {
25 15
        $this->originalFileName = trim($originalFileName);
26 15
        $this->originalFileName = !empty($this->originalFileName) ? $this->originalFileName : self::DEFAULT_FILE_NAME;
27 15
        $this->formatter = $formatter;
28 15
    }
29
30 27
    public function resolveEnvFileName(HostId $hostId): string
31
    {
32 27
        $resolvedFileName = $this->originalFileName;
33
34 27
        if ($this->formatter !== null) {
35 14
            $resolvedFileName = $this->formatter->formatName(trim($this->originalFileName, '.'), $hostId);
36
        }
37
38 27
        return $this->isDotShouldBeAddedToFileName($resolvedFileName) ? '.' . $resolvedFileName : $resolvedFileName;
39
    }
40
41 27
    private function isDotShouldBeAddedToFileName(string $resolvedFileName): bool
42
    {
43 27
        return strpos($this->originalFileName, '.') === 0 && strpos($resolvedFileName, '.') !== 0;
44
    }
45
}
46