Completed
Push — master ( d92f40...ed4867 )
by Jan Philipp
12s
created

ConfigFileFinder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverFiles() 0 6 1
A findFirstDirectoryWithConfigFile() 0 16 3
A determineResultInDirectory() 0 20 2
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
        $files = $this->findFirstDirectoryWithConfigFile($fromDirectory);
16
17
        return $this->determineResultInDirectory($files);
18
    }
19
20
    /**
21
     * @param string $fromDirectory
22
     * @return array
23
     */
24
    public function findFirstDirectoryWithConfigFile(string $fromDirectory): array
25
    {
26
        $currentDirectory = $fromDirectory;
27
28
        do {
29
            $globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB);
30
31
            if ($globResult) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $globResult of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
                return $globResult;
33
            }
34
35
            $currentDirectory = dirname($currentDirectory);
36
        } while ($currentDirectory !== '/');
37
38
        throw new \RuntimeException('No config file found, make sure you have created a .psh file');
39
    }
40
41
    /**
42
     * @param array $globResult
43
     * @return array
44
     */
45
    public function determineResultInDirectory(array $globResult): array
46
    {
47
        if (count($globResult) === 1) {
48
            return $globResult;
49
        }
50
51
        $overrideFile = array_filter($globResult, function (string $file) {
52
            $extension = pathinfo($file, PATHINFO_EXTENSION);
53
54
            return $extension === 'override';
55
        });
56
57
        $configFile = array_filter($globResult, function (string $file) {
58
            $extension = pathinfo($file, PATHINFO_EXTENSION);
59
60
            return $extension !== 'override';
61
        });
62
                
63
        return array_merge([$configFile[0]], $overrideFile);
64
    }
65
}
66