Completed
Push — master ( 76fb1d...cf725d )
by Andreas
07:54 queued 01:01
created

config::get_prefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 $tablecreate = false;
21
    public $tableupdate = false;
22
    public $testunit = false;
23
    public $midgardusername = 'admin';
24
    public $midgardpassword = 'password';
25
    public $authtype = '';
26
    public $pamfile = '';
27
    public $blobdir = '/var/lib/midgard2/blobs';
28
    public $sharedir = '/usr/share/midgard2';
29
    public $vardir = '/var/lib/midgard2';
30
    public $cachedir = '/var/cache/midgard2';
31
    public $gdathreads = false;
32
33 3
    public function read_file_at_path($path)
34
    {
35 3
        if (   !file_exists($path)
36 3
            || !is_readable($path)) {
37 1
            return false;
38
        }
39 2
        $parsed = parse_ini_file($path);
40
41 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

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

134
    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...
135
    {
136
    }
137
138
    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

138
    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...
139
    {
140
    }
141
142 1
    public function create_blobdir()
143
    {
144 1
        $subdirs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
145 1
        foreach ($subdirs as $dir) {
146 1
            foreach ($subdirs as $subdir) {
147 1
                if (   !is_dir($this->blobdir . '/' . $dir . '/' . $subdir)
148 1
                    && !mkdir($this->blobdir . '/' . $dir . '/' . $subdir, 0777, true)) {
149
                    return false;
150
                }
151 1
            }
152 1
        }
153
154 1
        return true;
155
    }
156
}
157