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

ConfigFileLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFileContents() 0 4 1
A fixPath() 0 23 3
A makeAbsolutePath() 0 4 1
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