Completed
Push — master ( dedab2...f146ce )
by Samuel
03:39 queued 02:35
created

Config::factory()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 12
nop 0
crap 42
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Console;
13
14
use Symfony\Component\Yaml\Parser;
15
16
class Config implements \ArrayAccess
17
{
18
    private $config = array(
19
        'adapter' => 'fastcgi',
20
        'fastcgi' => null,
21
        'temp_dir' => null
22 6
    );
23
24 6
    public function __construct(array $config = array())
25 2
    {
26
        if (!empty($config)) {
27 2
            $this->config = $config;
28 2
29 2
            if (!isset($this->config['temp_dir'])) {
30 2
                $this->config['temp_dir'] = sys_get_temp_dir();
31 6
            }
32
        }
33 6
    }
34
35 6
    public function offsetExists($offset)
36
    {
37
        return isset($this->config[$offset]);
38 5
    }
39
40 5
    public function offsetGet($offset)
41 5
    {
42
        if ($this->offsetExists($offset)) {
43 2
            return $this->config[$offset];
44
        }
45 3
    }
46
47 3
    public function offsetSet($offset, $value)
48 3
    {
49
        $this->config[$offset] = $value;
50 1
    }
51
52 1
    public function offsetUnset($offset)
53 1
    {
54
        unset($this->config[$offset]);
55
    }
56
57
    public static function factory() {
58
        $previous = null;
59
        $path = getcwd();
60
        $paths = array();
61
        $yaml = new Parser();
62
63
        while (($path = realpath($path)) && $path !== $previous) {
64
            $paths[] = "{$path}/.cachetool.yml";
65
            $previous = $path;
66
            $path .= '/../';
67
        }
68
69
        if ($home = static::getUserHomeDir()) {
70
          $paths[] = "{$home}/.cachetool.yml";
71
        }
72
73
        $paths[] = '/etc/cachetool.yml';
74
75
        foreach ($paths as $path) {
76
            if (is_file($path)) {
77
                $config = $yaml->parse(file_get_contents($path));
78
                return new Config($config);
79
            }
80
        }
81
82
        return new Config();
83
    }
84
85
    /**
86
     * Return the user's home directory.
87
     * From drush
88
     *
89
     * @return string
90
     */
91
    protected static function getUserHomeDir() {
0 ignored issues
show
Coding Style introduced by
getUserHomeDir uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
92
        // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
93
        // getenv('HOME') isn't set on Windows and generates a Notice.
94
        $home = getenv('HOME');
95
96
        if (!empty($home)) {
97
            // home should never end with a trailing slash.
98
            $home = rtrim($home, '/');
99
        } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
100
            // home on windows
101
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
102
            // If HOMEPATH is a root directory the path can end with a slash. Make sure
103
            // that doesn't happen.
104
            $home = rtrim($home, '\\/');
105
        }
106
107
        return empty($home) ? NULL : $home;
108
    }
109
}
110