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

ConfigFileLoaderFileSystemHandlers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 36
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
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