Completed
Push — master ( ad90dd...5d8ebc )
by Jan Philipp
02:11
created

ConfigFileFinder::preferNonDistributionFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Config;
5
6
/**
7
 * Resolve the path to the config file
8
 */
9
class ConfigFileFinder
10
{
11
    const VALID_FILE_NAME_GLOB = '.psh.*';
12
13
    public function discoverFile(string $fromDirectory): string
14
    {
15
        $currentDirectory = $fromDirectory;
16
        do {
17
            $globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB);
18
19
            if (count($globResult)) {
20
                return $this->preferNonDistributionFiles($globResult);
21
            }
22
23
            $currentDirectory = dirname($currentDirectory);
24
        } while ($currentDirectory !== '/');
25
26
        throw new \RuntimeException('No config file found, make sure you have created a .psh file');
27
    }
28
29
    /**
30
     * @param string[] $configFiles
31
     * @return string
32
     */
33
    private function preferNonDistributionFiles(array $configFiles): string
34
    {
35
        foreach ($configFiles as $file) {
36
            if (pathinfo($file, PATHINFO_EXTENSION) !== 'dist') {
37
                return $file;
38
            }
39
        }
40
41
        return $configFiles[0];
42
    }
43
}
44