Passed
Push — master ( 622e79...efafc0 )
by Andreas
03:31
created

config   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 94.03%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 83
c 2
b 0
f 0
dl 0
loc 130
ccs 63
cts 67
cp 0.9403
rs 10
wmc 23

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_prefix() 0 6 2
A read_file_at_path() 0 11 3
A read_file() 0 3 1
A save_file() 0 33 3
A apply_config() 0 21 5
A convert_to_storage() 0 8 4
A create_blobdir() 0 13 5
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
namespace midgard\portable\api;
8
9
class config
10
{
11
    public $dbtype = 'MySQL';
12
    public $database = 'midgard';
13
    public $port = 0;
14
    public $dbuser = '';
15
    public $dbpass = '';
16
    public $dbdir = '';
17
    public $host = 'localhost';
18
    public $logfilename = '';
19
    public $loglevel = 'warn';
20
    public $midgardusername = 'admin';
21
    public $midgardpassword = 'password';
22
    public $authtype = '';
23
    public $pamfile = '';
24
    public $blobdir = '/var/lib/midgard2/blobs';
25
    public $sharedir = '/usr/share/midgard2';
26
    public $vardir = '/var/lib/midgard2';
27
    public $cachedir = '/var/cache/midgard2';
28
29 3
    public function read_file_at_path($path)
30
    {
31 3
        if (   !file_exists($path)
32 3
            || !is_readable($path)) {
33 1
            return false;
34
        }
35 2
        $parsed = parse_ini_file($path);
36
37 2
        $this->apply_config($parsed);
0 ignored issues
show
Bug introduced by
It seems like $parsed can also be of type false; however, parameter $config of midgard\portable\api\config::apply_config() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $this->apply_config(/** @scrutinizer ignore-type */ $parsed);
Loading history...
38
39 2
        return true;
40
    }
41
42
    // TODO: find out if this could be moved to read_data()
43 2
    private function apply_config(array $config)
44
    {
45
        $mapping = [
46 2
            'type' => 'dbtype',
47
            'name' => 'database',
48
            'username' => 'dbuser',
49
            'password' => 'dbpass',
50
            'databasedir' => 'dbdir',
51
            'logfilename' => 'logfile',
52
        ];
53
54 2
        foreach ($config as $key => $value) {
55 2
            $key = strtolower($key);
56 2
            if (isset($mapping[$key])) {
57 2
                $key = $mapping[$key];
58
            }
59 2
            if (property_exists($this, $key)) {
60 2
                if (is_bool($this->$key)) {
61
                    $value = (boolean) $value;
62
                }
63 2
                $this->$key = $value;
64
            }
65
        }
66 2
    }
67
68 2
    private function get_prefix($user)
69
    {
70 2
        if ($user) {
71 1
            return getenv('HOME') . '/.midgard2/conf.d';
72
        }
73 1
        return '/etc/midgard2/conf.d';
74
    }
75
76 1
    public function read_file($name, $user = true) // <== TODO: check
77
    {
78 1
        return $this->read_file_at_path($this->get_prefix($user) . '/' . $name);
79
    }
80
81 1
    public function save_file($name, $user = true) // <== TODO: check
82
    {
83 1
        $prefix = $this->get_prefix($user);
84 1
        if (!file_exists($prefix)) {
85 1
            mkdir($prefix, 0777, true);
86
        }
87 1
        $filename = $prefix . '/' . $name;
88 1
        $contents = "[MidgardDir]\n\n";
89 1
        $contents .= $this->convert_to_storage('ShareDir', $this->sharedir);
90 1
        $contents .= $this->convert_to_storage('VarDir', $this->vardir);
91 1
        $contents .= $this->convert_to_storage('BlobDir', $this->blobdir);
92 1
        $contents .= $this->convert_to_storage('CacheDir', $this->cachedir);
93 1
        $contents .= "[Midgarddatabase]\n\n";
94 1
        $contents .= $this->convert_to_storage('Type', $this->dbtype);
95 1
        $contents .= $this->convert_to_storage('Host', $this->host);
96 1
        $contents .= $this->convert_to_storage('Port', $this->port);
97 1
        $contents .= $this->convert_to_storage('Name', $this->database);
98 1
        $contents .= $this->convert_to_storage('Username', $this->dbuser);
99 1
        $contents .= $this->convert_to_storage('Password', $this->dbpass);
100 1
        $contents .= $this->convert_to_storage('DatabaseDir', $this->dbdir);
101 1
        $contents .= "DefaultLanguage = pl\n\n";
102 1
        $contents .= $this->convert_to_storage('Logfile', $this->logfilename);
103 1
        $contents .= $this->convert_to_storage('Loglevel', $this->loglevel);
104 1
        $contents .= $this->convert_to_storage('MidgardUsername', $this->midgardusername);
105 1
        $contents .= $this->convert_to_storage('MidgardPassword', $this->midgardpassword);
106 1
        $contents .= $this->convert_to_storage('AuthType', $this->authtype);
107 1
        $contents .= $this->convert_to_storage('PamFile', $this->pamfile);
108
109 1
        $stat = file_put_contents($filename, $contents);
110 1
        if ($stat === false) {
111
            return false;
112
        }
113 1
        return true;
114
    }
115
116 1
    private function convert_to_storage($key, $value)
117
    {
118 1
        if (is_bool($value)) {
119
            $value = $value ? 'true' : 'false';
120 1
        } elseif ($value === '') {
121 1
            $value = '""';
122
        }
123 1
        return $key . ' = ' . $value . "\n\n";
124
    }
125
126 1
    public function create_blobdir()
127
    {
128 1
        $subdirs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
129 1
        foreach ($subdirs as $dir) {
130 1
            foreach ($subdirs as $subdir) {
131 1
                if (   !is_dir($this->blobdir . '/' . $dir . '/' . $subdir)
132 1
                    && !mkdir($this->blobdir . '/' . $dir . '/' . $subdir, 0777, true)) {
133
                    return false;
134
                }
135
            }
136
        }
137
138 1
        return true;
139
    }
140
}
141