Passed
Push — master ( d5fb39...3adbb6 )
by Andreas
03:35
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 16
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 21
ccs 16
cts 18
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($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 2
            'name' => 'database',
48 2
            'username' => 'dbuser',
49 2
            'password' => 'dbpass',
50 2
            'databasedir' => 'dbdir',
51 2
            'logfilename' => 'logfile',
52 2
        ];
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 2
            }
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 2
            }
65 2
        }
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 1
        }
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 1
        }
123 1
        return $key . ' = ' . $value . "\n\n";
124
    }
125
126
    public function read_data($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

126
    public function read_data(/** @scrutinizer ignore-unused */ $data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
    }
129
130
    public static function list_files($user = true) // <== TODO: check
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

130
    public static function list_files(/** @scrutinizer ignore-unused */ $user = true) // <== TODO: check

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
    }
133
134 1
    public function create_blobdir()
135
    {
136 1
        $subdirs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
137 1
        foreach ($subdirs as $dir) {
138 1
            foreach ($subdirs as $subdir) {
139 1
                if (   !is_dir($this->blobdir . '/' . $dir . '/' . $subdir)
140 1
                    && !mkdir($this->blobdir . '/' . $dir . '/' . $subdir, 0777, true)) {
141
                    return false;
142
                }
143 1
            }
144 1
        }
145
146 1
        return true;
147
    }
148
}
149