Settings   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 159
rs 10
c 1
b 0
f 0
ccs 58
cts 58
cp 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 14 2
A __set() 0 4 1
A __get() 0 4 1
A all() 0 4 1
A get() 0 4 1
A set() 0 11 2
A add() 0 6 1
A update() 0 6 1
A load() 0 10 3
A prepareDataForInsert() 0 9 2
A updateData() 0 11 3
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/settings-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\settings;
10
11
use Yii;
12
use yii\helpers\ArrayHelper;
13
use yii\db\Query;
14
15
/**
16
 * Component for storage settings in db
17
 */
18
class Settings extends \yii\base\Component
19
{
20
    /**
21
     * @var string $tableName Table name
22
     */
23
    public $tableName = 'settings';
24
    /**
25
     * @var string $cacheName A key identifying the values to be cached
26
     */
27
    public $cacheName = 'settings';
28
    /**
29
     * @var string $cache Cache component name
30
     */
31
    public $cache = 'cache';
32
    /**
33
     * @var array $data
34
     */
35
    private $data = [];
36
37 1
    public function __set($key, $value)
38
    {
39 1
        return $this->set($key, $value);
40
    }
41
42 1
    public function __get($key)
43
    {
44 1
        return $this->get($key);
45
    }
46
47 1
    public function init()
48
    {
49 1
        parent::init();
50
51 1
        $data = Yii::$app->{$this->cache}->get($this->cacheName);
52
53 1
        if ($data) {
54 1
            $this->data = unserialize($data);
55 1
            return;
56
        }
57 1
        $data = (new Query())->select('*')->from($this->tableName)->all();
58 1
        $data = ArrayHelper::map($data, 'key', 'value');
59 1
        $this->updateData($data);
60 1
    }
61
62
    /**
63
     * Get all data
64
     *
65
     * @return array
66
     */
67 3
    public function all()
68
    {
69 3
        return $this->data;
70
    }
71
72
    /**
73
     * Get value by key
74
     *
75
     * @param string $key
76
     * @return string
77
     */
78 2
    public function get($key)
79
    {
80 2
        return ArrayHelper::getValue($this->data, $key, null);
81
    }
82
83
    /**
84
     * Set value
85
     *
86
     * @param string $key
87
     * @param string $value
88
     * @SuppressWarnings(PHPMD.ElseExpression)
89
     */
90 1
    public function set($key, $value)
91
    {
92 1
        if (array_key_exists($key, $this->data)) {
93 1
            $this->update($key, $value);
94 1
        } else {
95 1
            $this->add($key, $value);
96
        }
97
98 1
        $this->data[$key] = $value;
99 1
        $this->updateData($this->data);
100 1
    }
101
102
    /**
103
     * Add setting
104
     *
105
     * @param string $key
106
     * @param string $value
107
     */
108 1
    private function add($key, $value)
109
    {
110 1
        Yii::$app->db
111 1
            ->createCommand()
112 1
            ->insert($this->tableName, ['key' => $key, 'value' => $value])->execute();
113 1
    }
114
115
    /**
116
     * Update setting
117
     *
118
     * @param string $key
119
     * @param string $value
120
     */
121 1
    private function update($key, $value)
122
    {
123 1
        Yii::$app->db
124 1
            ->createCommand()
125 1
            ->update($this->tableName, ['value' => $value], '`key` = :key', [':key' => $key])->execute();
126 1
    }
127
128
    /**
129
     * Load data
130
     *
131
     * @param array $data
132
     */
133 4
    public function load($data)
134
    {
135 4
        $db = Yii::$app->db->createCommand();
136 4
        $db->truncateTable($this->tableName)->execute();
137
138 4
        if (is_array($data) && count($data)) {
139 4
            $db->batchInsert($this->tableName, ['key', 'value'], $this->prepareDataForInsert($data))->execute();
140 4
        }
141 4
        $this->updateData($data);
142 4
    }
143
144
    /**
145
     * Prepare data for insert
146
     *
147
     * @param array $data
148
     * @return array
149
     */
150 4
    private function prepareDataForInsert($data)
151
    {
152 4
        $items = [];
153 4
        foreach ($data as $key => $value) {
154 4
            $items[] = ['key' => $key, 'value' => $value];
155 4
        }
156
157 4
        return $items;
158
    }
159
160
    /**
161
     * Update data and cache
162
     *
163
     * @param array $data
164
     */
165 4
    private function updateData($data)
166
    {
167 4
        $this->data = $data;
168
169 4
        $cache = Yii::$app->{$this->cache};
170 4
        $cache->delete($this->cacheName);
171
172 4
        if (is_array($this->data) && count($this->data)) {
173 4
            $cache->set($this->cacheName, serialize($this->data));
174 4
        }
175 4
    }
176
}
177