Completed
Push — master ( d234f3...0a4926 )
by Jan Philipp
47s queued 12s
created

loadFileContents()   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 InvalidArgumentException;
6
use function file_exists;
7
use function file_get_contents;
8
use function pathinfo;
9
use function print_r;
10
use function sprintf;
11
12
trait ConfigFileLoaderFileSystemHandlers
13
{
14
    protected function loadFileContents(string $file): string
15
    {
16
        return file_get_contents($file);
17
    }
18
19
    protected function fixPath(
20
        string $applicationRootDirectory,
21
        string $absoluteOrRelativePath,
22
        string $baseFile
23
    ): string {
24
        $possiblyValidFiles = [
25
            $applicationRootDirectory . '/' . $absoluteOrRelativePath,
26
            $this->makeAbsolutePath($baseFile, $absoluteOrRelativePath),
27
            $absoluteOrRelativePath,
28
        ];
29
30
        foreach ($possiblyValidFiles as $file) {
31
            if (file_exists($file)) {
32
                return $file;
33
            }
34
        }
35
36
        throw new InvalidArgumentException(sprintf(
37
            'Unable to find a file referenced by "%s", tried: %s',
38
            $absoluteOrRelativePath,
39
            print_r($possiblyValidFiles, true)
40
        ));
41
    }
42
43
    protected function makeAbsolutePath(string $baseFile, string $path): string
44
    {
45
        return pathinfo($baseFile, PATHINFO_DIRNAME) . '/' . $path;
46
    }
47
}
48