Completed
Push — master ( 6e4673...8c6df7 )
by Leandro
01:38
created

src/Settings.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Soluto\Settings;
4
5
use Yii;
6
use yii\base\Component;
7
use yii\db\Connection;
8
use yii\db\Query;
9
use yii\base\Event;
10
11
class Settings extends Component
12
{
13
    /**
14
     * @event SettingsEvent an event that is triggered before execute command.
15
     */
16
    const EVENT_BEFORE_EXECUTE = 'beforeExecute';
17
18
    /**
19
     * @var string Name of the table where configurations will be stored
20
     */
21
    public $tableName = '{{%setting}}';
22
23
    /**
24
     * @var string Name of column where keys will be stored
25
     */
26
    public $keyColumnName = 'key';
27
28
    /**
29
     * @var string Name of column where values will be stored
30
     */
31
    public $valueColumnName = 'value';
32
33
    /**
34
     * @return Connection the DB connection instance
35
     */
36 2
    protected function getDb()
37
    {
38 2
        return Yii::$app->getDb();
39
    }
40
41
    /**
42
     * Whether the configuration exists in the database
43
     * @param string $name configuration name
44
     * @param integer $tenantId The tenant id value
45
     * @return bool
46
     */
47 2
    protected function exists($name)
48
    {
49 2
        $query = $this->createQuery($name);
50 2
        return $query->exists();
51
    }
52
53
    /**
54
     * Returns configuration value from database
55
     * @param string $name configuration name
56
     * @return string value stored in database
57
     */
58 1
    public function get($name, $defaultValue = null)
59
    {
60 1
        $value = null;
61 1
        $query = $this->createQuery($name);
62 1
        $row = $query->one($this->getDb());
63
64 1
        $value = ($row) ? $row[$this->valueColumnName] : null;
65
66 1
        if (is_string($value) && trim($value) == '') {
67
            $value = null;
68
        }
69
70 1
        return ($value !== null) ? $value : $defaultValue;
71
    }
72
73
    /**
74
     * Store configuration value to database
75
     * @param string $name
76
     * @param mixed $value
77
     */
78 2
    public function set($name, $value)
79
    {
80 2
        $db = $this->getDb();
81 2
        $values = [$this->valueColumnName => $value];
82 2
        $where = [$this->keyColumnName => $name];
83
84 2
        $event = $this->beforeExecute();
85 2
        if ($event->data) {
86 1
            $values = array_merge($event->data, $values);
87 1
            $where = array_merge($event->data, $where);
88
        }
89
90 2
        if ($this->exists($name)) {
91 1
            $db->createCommand()
92 1
                ->update($this->tableName, $values, $where)
93 1
                ->execute();
94
        } else  {
95 2
            $values = array_merge($values, $where);
96
97 2
            $db->createCommand()
98 2
                ->insert($this->tableName, $values)
99 2
                ->execute();
100
        }
101 2
    }
102
103
    /**
104
     * Retrieves all configurations stored in database
105
     * @param integer $tenantId
106
     * @return array
107
     */
108 1
    public function all()
109
    {
110 1
        $result = [];
111
112 1
        $query = $this->createQuery()
113 1
            ->addSelect($this->keyColumnName);
114
115 1
        $rows = $query->all($this->getDb());
116
117 1
        foreach ($rows as $row) {
118 1
            $result[$row[$this->keyColumnName]] = $row[$this->valueColumnName];
119
        }
120
121 1
        return $result;
122
    }
123
124
    /**
125
     * Store all configuration in database
126
     * @param array $names
127
     */
128 1
    public function save($names)
129
    {
130 1
        foreach ($names as $key => $value) {
131 1
            $this->set($key, $value) ;
132
        }
133 1
    }
134
135
    /**
136
     * Remove specified setting
137
     * @param array|string $name
138
     */
139 1
    public function remove($name)
140
    {
141 1
        $where = [$this->keyColumnName => $name];
142
143 1
        $event = $this->beforeExecute();
144 1
        if ($event->data) {
145
            $where = array_merge($event->data, $where);
146
        }
147
148 1
        $this->getDb()
149 1
            ->createCommand()
150 1
            ->delete($this->tableName, $where)
151 1
            ->execute();
152 1
    }
153
154
    /**
155
     * Removes all settings
156
     */
157 1
    public function removeAll()
158
    {
159 1
        $event = $this->beforeExecute();
160 1
        $where = $event->data ? $event->data : '';
161
162 1
        $this->getDb()
163 1
            ->createCommand()
164 1
            ->delete($this->tableName, $where)
165 1
            ->execute();
166 1
    }
167
168
    /**
169
     * Creates query to find settings value
170
     * @param string $name
171
     * @return \yii\db\Query
172
     */
173 2
    protected function createQuery($name = null)
174
    {
175 2
        $query = (new Query())
176 2
            ->select([$this->valueColumnName])
177 2
            ->from($this->tableName);
178
179 2
        $event = $this->beforeExecute();
180
181 2
        if ($event->data) {
182 1
            $query->andWhere($event->data);
183
        }
184
185 2
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
186 2
            $query->andWhere([$this->keyColumnName => $name]);
187
        }
188
189 2
        return $query;
190
    }
191
192
    /**
193
     * This method is called at the before execute db command
194
     * @return yii\base\Event
195
     */
196 2
    protected function beforeExecute()
197
    {
198 2
        $event = new Event();
199 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
200 2
        return $event;
201
    }
202
203
}
204