Completed
Pull Request — master (#42)
by Simon
01:46
created

ConfigFileFinder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverFiles() 0 15 3
A preferNonDistributionFiles() 0 10 3
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
    /**
14
     * @param string $fromDirectory
15
     * @return String[]
16
     */
17
    public function discoverFiles(string $fromDirectory): array
18
    {
19
        $currentDirectory = $fromDirectory;
20
        do {
21
            $globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB);
22
23
            if (count($globResult)) {
24
                return [ $this->preferNonDistributionFiles($globResult) ];
25
            }
26
27
            $currentDirectory = dirname($currentDirectory);
28
        } while ($currentDirectory !== '/');
29
30
        throw new \RuntimeException('No config file found, make sure you have created a .psh file');
31
    }
32
33
    /**
34
     * @param string[] $configFiles
35
     * @return string
36
     */
37
    private function preferNonDistributionFiles(array $configFiles): string
38
    {
39
        foreach ($configFiles as $file) {
40
            if (pathinfo($file, PATHINFO_EXTENSION) !== 'dist') {
41
                return $file;
42
            }
43
        }
44
45
        return $configFiles[0];
46
    }
47
}
48