Passed
Branch master (b2f909)
by Andreas
04:01
created

config::apply_config()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

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