Completed
Push — master ( 937114...74cc4f )
by Thomas
14s
created

ConfigFileFinder::discoverFiles()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 7
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 discoverFiles(string $fromDirectory): array
14
    {
15
        $configFiles = [];
16
        $currentDirectory = $fromDirectory;
17
18
        do {
19
            $globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB);
20
21
            if (count($globResult) === 1) {
22
                return $globResult;
23
            }
24
25
            if (count($globResult) > 1) {
26
                foreach ($globResult as $configFile) {
27
                    $extension = pathinfo($configFile, PATHINFO_EXTENSION);
28
                    if (!in_array($extension, ['dist', 'override'], true)) {
29
                        $configFiles[0] = $configFile;
30
                    } elseif ($extension === 'dist' && !isset($configFiles[0])) {
31
                        $configFiles[0] = $configFile;
32
                    } elseif ($extension === 'override') {
33
                        $configFiles[1] = $configFile;
34
                    }
35
                }
36
37
                return $configFiles;
38
            }
39
40
            $currentDirectory = dirname($currentDirectory);
41
        } while ($currentDirectory !== '/');
42
43
        throw new \RuntimeException('No config file found, make sure you have created a .psh file');
44
    }
45
}
46