Completed
Pull Request — master (#111)
by Jan Philipp
03:12 queued 11s
created

ConfigFileLoaderFileSystemHandlers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFileContents() 0 4 1
A fixPath() 0 19 3
A makeAbsolutePath() 0 8 2
A getWorkingDir() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use function file_exists;
6
use function file_get_contents;
7
use function pathinfo;
8
9
trait ConfigFileLoaderFileSystemHandlers
10
{
11
    protected function loadFileContents(string $file): string
12
    {
13
        return file_get_contents($file);
14
    }
15
16
    protected function fixPath(
17
        string $applicationRootDirectory,
18
        string $absoluteOrRelativePath,
19
        string $baseFile
20
    ): string {
21
        $possiblyValidFiles = [
22
            $applicationRootDirectory . '/' . $absoluteOrRelativePath,
23
            $this->makeAbsolutePath($baseFile, $absoluteOrRelativePath),
24
            $absoluteOrRelativePath,
25
        ];
26
27
        foreach ($possiblyValidFiles as $file) {
28
            if (file_exists($file)) {
29
                return $file;
30
            }
31
        }
32
33
        throw new InvalidReferencedPath($absoluteOrRelativePath, $possiblyValidFiles);
34
    }
35
36
    protected function makeAbsolutePath(string $baseFile, string $path): string
37
    {
38
        if ($path[0] === DIRECTORY_SEPARATOR) {
39
            return $path;
40
        }
41
42
        return $this->getWorkingDir($baseFile) . '/' . $path;
43
    }
44
45
    protected function getWorkingDir(string $baseFile): string
46
    {
47
        return pathinfo($baseFile, PATHINFO_DIRNAME);
48
    }
49
}
50