ConfigLocator::locate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 11
cp 0.6364
rs 9.9332
cc 4
nc 4
nop 2
crap 4.7691
1
<?php
2
3
4
namespace Deployee\Components\Config;
5
6
7
class ConfigLocator
8
{
9
    /**
10
     * @var array
11
     */
12
    private static $possibleConfigFileNames = [
13
        '.deployee.{env}.yml',
14
        '.deployee.yml',
15
        'deployee.{env}.yml',
16
        'deployee.yml',
17
        '.deployee.{env}.dist.yml',
18
        '.deployee.dist.yml',
19
        'deployee.{env}.dist.yml',
20
        'deployee.dist.yml',
21
    ];
22
23
    /**
24
     * @param array $searchableDirs
25
     * @param string $env
26
     * @return string
27
     */
28 1
    public function locate(array $searchableDirs, string $env = ''): string
29
    {
30 1
        foreach ($searchableDirs as $expectedDir) {
31 1
            foreach(self::$possibleConfigFileNames as $possibleConfigFileName){
32 1
                $possibleConfigFileName = str_replace('{env}', $env, $possibleConfigFileName);
33 1
                $expectedFilepath = $expectedDir . DIRECTORY_SEPARATOR . $possibleConfigFileName;
34 1
                if(is_file($expectedFilepath)){
35 1
                    return realpath($expectedDir) . DIRECTORY_SEPARATOR . $possibleConfigFileName;
36
                }
37
            }
38
        }
39
40
        throw new \RuntimeException(
41
            sprintf(
42
                'Could not find config file (%s)',
43
                implode(', ', self::$possibleConfigFileNames)
44
            )
45
        );
46
    }
47
}