Config   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 48%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 107
ccs 24
cts 50
cp 0.48
rs 10
c 3
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetUnset() 0 3 1
A offsetGet() 0 4 2
A getUserHomeDir() 0 18 5
A offsetExists() 0 3 1
A factory() 0 28 6
A fromFile() 0 4 1
A offsetSet() 0 3 1
A toJSON() 0 3 1
A __construct() 0 7 3
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 = [
19
        'adapter' => 'fastcgi',
20
        'extensions' => ['apcu', 'opcache'],
21
        'fastcgi' => null,
22
        'temp_dir' => null
23
    ];
24
25 27
    public function __construct(array $config = [])
26
    {
27 27
        if (!empty($config)) {
28 19
            $this->config = array_replace($this->config, $config);
29
30 19
            if (!isset($this->config['temp_dir'])) {
31 18
                $this->config['temp_dir'] = sys_get_temp_dir();
32
            }
33
        }
34 27
    }
35
36 27
    public function offsetExists($offset)
37
    {
38 27
        return isset($this->config[$offset]);
39
    }
40
41 26
    public function offsetGet($offset)
42
    {
43 26
        if ($this->offsetExists($offset)) {
44 26
            return $this->config[$offset];
45
        }
46 9
    }
47
48 8
    public function offsetSet($offset, $value)
49
    {
50 8
        $this->config[$offset] = $value;
51 8
    }
52
53 1
    public function offsetUnset($offset)
54
    {
55 1
        unset($this->config[$offset]);
56 1
    }
57
58 23
    public function toJSON()
59
    {
60 23
        return json_encode($this->config);
61
    }
62
63
    public static function factory()
64
    {
65
        $previous = null;
66
        $path = getcwd();
67
        $paths = [];
68
69
        while (($path = realpath($path)) && $path !== $previous) {
70
            $paths[] = "{$path}/.cachetool.yml";
71
            $paths[] = "{$path}/.cachetool.yaml";
72
            $previous = $path;
73
            $path .= '/../';
74
        }
75
76
        if ($home = static::getUserHomeDir()) {
77
            $paths[] = "{$home}/.cachetool.yml";
78
            $paths[] = "{$home}/.cachetool.yaml";
79
        }
80
81
        $paths[] = '/etc/cachetool.yml';
82
        $paths[] = '/etc/cachetool.yaml';
83
84
        foreach ($paths as $path) {
85
            if (is_file($path)) {
86
                return static::fromFile($path);
87
            }
88
        }
89
90
        return new Config();
91
    }
92
93 2
    public static function fromFile($path) {
94 2
        $yaml = new Parser();
95 2
        $config = $yaml->parse(file_get_contents($path));
96 2
        return new Config($config);
97
    }
98
99
    /**
100
     * Return the user's home directory.
101
     * From drush
102
     *
103
     * @return string
104
     */
105
    protected static function getUserHomeDir()
106
    {
107
        // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
108
        // getenv('HOME') isn't set on Windows and generates a Notice.
109
        $home = getenv('HOME');
110
111
        if (!empty($home)) {
112
            // home should never end with a trailing slash.
113
            $home = rtrim($home, '/');
114
        } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
115
            // home on windows
116
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
117
            // If HOMEPATH is a root directory the path can end with a slash. Make sure
118
            // that doesn't happen.
119
            $home = rtrim($home, '\\/');
120
        }
121
122
        return empty($home) ? null : $home;
123
    }
124
}
125