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

ConfigFileLoader::makeAbsolutePath()   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 2
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
abstract class ConfigFileLoader implements ConfigLoader
6
{
7
8
    /**
9
     * @param string $file
10
     * @return string
11
     */
12
    protected function loadFileContents(string $file): string
13
    {
14
        return file_get_contents($file);
15
    }
16
17
    /**
18
     * @param string $applicationRootDirectory
19
     * @param string $absoluteOrRelativePath
20
     * @param string $baseFile
21
     * @return string
22
     */
23
    protected function fixPath(
24
        string $applicationRootDirectory,
25
        string $absoluteOrRelativePath,
26
        string $baseFile
27
    ): string {
28
        $possiblyValidFiles = [
29
            $applicationRootDirectory . '/' . $absoluteOrRelativePath,
30
            $this->makeAbsolutePath($baseFile, $absoluteOrRelativePath),
31
            $absoluteOrRelativePath,
32
        ];
33
34
        foreach ($possiblyValidFiles as $file) {
35
            if (file_exists($file)) {
36
                return $file;
37
            }
38
        }
39
40
        throw new \InvalidArgumentException(sprintf(
41
            'Unable to find a file referenced by "%s", tried: %s',
42
            $absoluteOrRelativePath,
43
            print_r($possiblyValidFiles, true)
44
        ));
45
    }
46
47
    /**
48
     * @param string $baseFile
49
     * @param string $path
50
     * @return string
51
     */
52
    protected function makeAbsolutePath(string $baseFile, string $path): string
53
    {
54
        return pathinfo($baseFile, PATHINFO_DIRNAME) . '/' . $path;
55
    }
56
}
57