Completed
Push — master ( 1ece6c...656998 )
by Jan Philipp
13s queued 11s
created

ConfigFileLoader::fixPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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