Completed
Pull Request — master (#111)
by Jan Philipp
02:02
created

getWorkingDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use function file_exists;
6
use function file_get_contents;
7
use function pathinfo;
8
9
trait ConfigFileLoaderFileSystemHandlers
10
{
11
    protected function loadFileContents(string $file): string
12
    {
13
        return file_get_contents($file);
14
    }
15
16
    protected function fixPath(
17
        string $applicationRootDirectory,
18
        string $absoluteOrRelativePath,
19
        string $baseFile
20
    ): string {
21
        $possiblyValidFiles = [
22
            $applicationRootDirectory . '/' . $absoluteOrRelativePath,
23
            $this->makeAbsolutePath($baseFile, $absoluteOrRelativePath),
24
            $absoluteOrRelativePath,
25
        ];
26
27
        foreach ($possiblyValidFiles as $file) {
28
            if (file_exists($file)) {
29
                return $file;
30
            }
31
        }
32
33
        throw new InvalidReferencedPath($absoluteOrRelativePath, $possiblyValidFiles);
34
    }
35
36
    protected function makeAbsolutePath(string $baseFile, string $path): string
37
    {
38
        if ($path[0] === DIRECTORY_SEPARATOR) {
39
            return $path;
40
        }
41
42
        return $this->getWorkingDir($baseFile) . '/' . $path;
43
    }
44
45
    protected function getWorkingDir(string $baseFile): string
46
    {
47
        return pathinfo($baseFile, PATHINFO_DIRNAME);
48
    }
49
}
50