Completed
Push — master ( 451c37...6080fd )
by personal
19:14 queued 12:05
created

ConfigLocator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Config;
11
12
/**
13
 * Config locator
14
 *
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class ConfigLocator
18
{
19
    /**
20
     * Default files to check
21
     * @var array
22
     */
23
    private $defaults = array();
24
25
    /**
26
     * Constructor
27
     *
28
     * @param array $defaults
29
     */
30
    public function __construct(array $defaults = null) {
31
        if(is_null($defaults)) {
32
            $defaults = array('.phpmetrics.yml', '.phpmetrics.yml.dist', '.phpmetrics-dist.yml');
33
        }
34
        $this->defaults = $defaults;
35
    }
36
37
    /**
38
     * Locates file. Il no file is provided, it will search for default .phpmetrics.yml file
39
     *
40
     * @param $filename
41
     * @return string
42
     */
43
    public function locate($filename) {
44
45
        if(null === $filename) {
46
            // try to use default configfile : .phpmetrics.yml or .phpmetrics.yml.dist
47
            foreach($this->defaults as $filenameToCheck) {
48
                $filenameToCheck = getcwd().DIRECTORY_SEPARATOR.$filenameToCheck;
49
                if (true === $this->isFileAccessible($filenameToCheck)) {
50
                    return $filenameToCheck;
51
                }
52
            }
53
        }
54
55
        if(false === $this->isFileAccessible($filename)) {
56
            throw new \RuntimeException('configuration file is not accessible');
57
        }
58
59
        return $filename;
60
    }
61
    
62
    private function isFileAccessible($filename)
63
    {
64
        return \file_exists($filename) && \is_readable($filename);
65
    }
66
}