| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 33.33% covered (danger) | 33.33% | 1 / 3 | CRAP | 46.43% covered (danger) | 46.43% | 13 / 28
Config | 0.00% covered (danger) | 0.00% | 0 / 1 | 33.33% covered (danger) | 33.33% | 1 / 3 | 21.45 | 46.43% covered (danger) | 46.43% | 13 / 28
install | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 6 / 6
setConfig | 0.00% covered (danger) | 0.00% | 0 / 1 | 6 | 0.00% covered (danger) | 0.00% | 0 / 14
getConfig | 0.00% covered (danger) | 0.00% | 0 / 1 | 5.05 | 87.50% covered (warning) | 87.50% | 7 / 8

1 | <?php
2 |
3 | namespace Entity;
4 |
5 | class Config extends ModelBase
6 | {
7 | private static $config = array();
8 |
9 | protected function install()
10 | {
11 | $dbPrefix = $this->getDbPrefix();
12 | $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}config` (
13 | `lang` VARCHAR(8) NULL,
14 | `key` VARCHAR(64) NOT NULL,
15 | `value` TEXT NULL,
16 | UNIQUE(`lang`, `key`)
17 | )");
18 | if ($result === false)
19 | throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
20 | return true;
21 | }
22 |
23 | public static function setConfig($lang, $key, $value)
24 | {
25 | $fetcher = new self();
26 | $data = $fetcher->selects(array("lang" => $lang, "key" => $key));
27 | if (empty($data))
28 | {
29 | $data = new self();
30 | $data->lang = $lang;
31 | $data->key = $key;
32 | $data->value = $value;
33 | $data->save();
34 | }
35 | else
36 | {
37 | $data = $data[0];
38 | $data->value = $value;
39 | $data->save();
40 | }
41 | }
42 |
43 | public static function getConfig($lang =null, $key =null, $defaultValue =null)
44 | {
45 | $fetcher = new self();
46 | if (isset(self::$config[$lang]))
47 | return;
48 | $values = $fetcher->selects(array("lang" => $lang));
49 | foreach ($values as $i)
50 | self::$config[$lang][$i->key] = $i->value;
51 | if ($key)
52 | return (isset(self::$config[$lang][$key]) ? self::$config[$lang][$key] : $defaultValue);
53 | return $defaultValue;
54 | }
55 | }
56 |