ServerConfig::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 3
b 1
f 0
nc 2
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
namespace Sovereign\Lib;
3
4
/**
5
 * Class ServerConfig
6
 * @package Sovereign\Lib
7
 */
8
class ServerConfig
9
{
10
    /**
11
     * @var Db
12
     */
13
    protected $db;
14
15
    /**
16
     * ServerConfig constructor.
17
     * @param Db $db
18
     */
19
    public function __construct(Db $db)
20
    {
21
        $this->db = $db;
22
    }
23
24
    /**
25
     * @param string $key
26
     */
27
    public function get($guildID, $key)
28
    {
29
        $data = json_decode($this->db->queryField("SELECT settings FROM settings WHERE serverID = :serverID", "settings", array(":serverID" => $guildID)));
30
        return isset($data->{$key}) ? $data->{$key} : null;
31
    }
32
33
    /**
34
     * @param string $key
35
     */
36
    public function set($guildID, $key, $value)
37
    {
38
        $json = $this->getAll($guildID);
39
        $json->{$key} = $value;
40
        $data = json_encode($json);
41
        $this->db->execute("INSERT INTO settings (serverID, settings) VALUES (:serverID, :settings) ON DUPLICATE KEY UPDATE settings = :settings", array(":serverID" => $guildID, ":settings" => $data));
42
    }
43
44
    /**
45
     * @param $guildID
46
     * @return mixed
47
     */
48
    public function getAll($guildID)
49
    {
50
        return json_decode($this->db->queryField("SELECT settings FROM settings WHERE serverID = :serverID", "settings", array(":serverID" => $guildID)));
51
    }
52
53
}