Passed
Push — master ( 72f8f4...a05bce )
by Jesús
07:47 queued 11s
created

WithSuffixAbsolutePathStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\PathNormalizer;
6
7
final class WithSuffixAbsolutePathStrategy implements AbsolutePathStrategyInterface
8
{
9
    private string $appRootDir;
10
11
    private string $configFileNameSuffix;
12
13 28
    public function __construct(
14
        string $appRootDir,
15
        string $configFileNameSuffix = ''
16
    ) {
17 28
        $this->appRootDir = $appRootDir;
18 28
        $this->configFileNameSuffix = $configFileNameSuffix;
19 28
    }
20
21 28
    public function generateAbsolutePath(string $relativePath): string
22
    {
23 28
        $suffix = $this->configFileNameSuffix;
24 28
        if ($suffix === '') {
25 24
            return '';
26
        }
27
28
        // place the file suffix right before the file extension
29 4
        $dotPos = strpos($relativePath, '.');
30
31 4
        if ($dotPos !== false) {
32 3
            $relativePathWithFileSuffix = substr($relativePath, 0, $dotPos)
33 3
                . '-' . $suffix
34 3
                . substr($relativePath, $dotPos);
35
        } else {
36 1
            $relativePathWithFileSuffix = $relativePath . '-' . $suffix;
37
        }
38
39 4
        return sprintf(
40 4
            '%s/%s',
41 4
            rtrim($this->appRootDir, '/'),
42 4
            ltrim($relativePathWithFileSuffix, '/')
43
        );
44
    }
45
}
46