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

ConfigFileFinder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverFile() 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
    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