Completed
Push — master ( ff18d3...b1af96 )
by Loban
02:26
created

DbStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 75
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A getValue() 0 10 1
A setValue() 0 21 3
A deleteValue() 0 8 1
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\storage;
10
11
use Yii;
12
use yii\db\Query;
13
use yii\db\Connection;
14
use yii\di\Instance;
15
use yii\base\BaseObject;
16
17
/**
18
 * Class DbStorage
19
 * @package lav45\settings\storage
20
 */
21
class DbStorage extends BaseObject implements StorageInterface
22
{
23
    /**
24
     * @var Connection|array|string
25
     */
26
    public $db = 'db';
27
    /**
28
     * @var string
29
     */
30
    public $tableName = '{{%settings}}';
31
32
    /**
33
     * Initializes the application component.
34
     */
35 1
    public function init()
36
    {
37 1
        parent::init();
38 1
        $this->db = Instance::ensure($this->db, Connection::class);
39 1
    }
40
41
    /**
42
     * @param string $key
43
     * @return false|null|string
44
     */
45 1
    public function getValue($key)
46
    {
47 1
        return (new Query())
48 1
            ->select(['data'])
49 1
            ->from($this->tableName)
50 1
            ->where(['id' => $key])
51 1
            ->limit(1)
52 1
            ->createCommand($this->db)
53 1
            ->queryScalar();
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param string $value
59
     * @return bool
60
     */
61 1
    public function setValue($key, $value)
62
    {
63 1
        $exists = (new Query())
64 1
            ->from($this->tableName)
65 1
            ->where(['id' => $key])
66 1
            ->exists($this->db);
67
68 1
        $query = (new Query())->createCommand($this->db);
69
70
        try {
71 1
            if ($exists) {
72
                $query->update($this->tableName, ['data' => $value], ['id' => $key])->execute();
73
            } else {
74 1
                $query->insert($this->tableName, ['id' => $key, 'data' => $value])->execute();
75
            }
76
        } catch (\Exception $e) {
77
            Yii::error(get_class($e) . '[' . $e->getCode() . '] ' . $e->getMessage());
78
            return false;
79
        }
80 1
        return true;
81
    }
82
83
    /**
84
     * @param string $key
85
     * @return bool
86
     */
87 1
    public function deleteValue($key)
88
    {
89 1
        $result = (new Query())->createCommand($this->db)
90 1
            ->delete($this->tableName, ['id' => $key])
91 1
            ->execute();
92
93 1
        return $result > 0;
94
    }
95
}
96