Completed
Push — master ( 7a204f...72d397 )
by Leandro
01:44
created

src/Settings.php (2 issues)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

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
10
class Settings extends Component
11
{
12
    /**
13
     * @event SettingsEvent an event that is triggered before execute command.
14
     */
15
    const EVENT_BEFORE_EXECUTE = 'beforeExecute';
16
17
    /**
18
     * @var string Name of the table where configurations will be stored
19
     */
20
    public $tableName = '{{%settings}}';
21
22
    /**
23
     * @var string Name of column where keys will be stored
24
     */
25
    public $keyColumnName = 'key';
26
27
    /**
28
     * @var string Name of column where values will be stored
29
     */
30
    public $valueColumnName = 'value';
31
32
    /**
33
     * @return Connection the DB connection instance
34
     */
35 2
    protected function getDb()
36
    {
37 2
        return Yii::$app->getDb();
38
    }
39
40
    /**
41
     * Whether the configuration exists in the database
42
     * @param string $name configuration name
43
     * @param integer $tenantId The tenant id value
44
     * @return bool
45
     */
46 2
    protected function exists($name)
47
    {
48 2
        $query = $this->createQuery($name);
49 2
        return $query->exists();
50
    }
51
52
    /**
53
     * Returns configuration value from database
54
     * @param string $name configuration name
55
     * @return string value stored in database
56
     */
57 1
    public function get($name, $defaultValue = null)
58
    {
59 1
        $value = null;
60 1
        $query = $this->createQuery($name);
61 1
        $row = $query->one($this->getDb());
62
63 1
        $value = ($row) ? $row[$this->valueColumnName] : null;
64
65 1
        if (is_string($value) && trim($value) == '') {
66
            $value = null;
67
        }
68
69 1
        return ($value !== null) ? $value : $defaultValue;
70
    }
71
72
    /**
73
     * Store configuration value to database
74
     * @param string $name
75
     * @param mixed $value
76
     */
77 2
    public function set($name, $value)
78
    {
79 2
        $db = $this->getDb();
80
81 2
        $event = new SettingsEvent();
82 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
83
84 2
        $columns = array_merge($event->columns, [$this->valueColumnName => $value]);
85 2
        $where = array_merge($event->columns, [$this->keyColumnName => $name]);
86
87 2
        if ($this->exists($name)) {
88 1
            $db->createCommand()
89 1
                ->update($this->tableName, $columns, $where)
90 1
                ->execute();
91 1
        } else  {
92 2
            $values = array_merge([$this->keyColumnName => $name], $columns);
93
94 2
            $db->createCommand()
95 2
                ->insert($this->tableName, $values)
96 2
                ->execute();
97
        }
98 2
    }
99
100
    /**
101
     * Retrieves all configurations stored in database
102
     * @param integer $tenantId
103
     * @return array
104
     */
105 1
    public function all()
106
    {
107 1
        $result = [];
108
109 1
        $query = $this->createQuery()
110 1
            ->addSelect($this->keyColumnName);
111
112 1
        $rows = $query->all($this->getDb());
113
114 1
        foreach ($rows as $row) {
115 1
            $result[$row[$this->keyColumnName]] = $row[$this->valueColumnName];
116 1
        }
117
118 1
        return $result;
119
    }
120
121
    /**
122
     * Store all configuration in database
123
     * @param array $names
124
     */
125 1
    public function save($names)
126
    {
127 1
        foreach ($names as $key => $value) {
128 1
            $this->set($key, $value) ;
129 1
        }
130 1
    }
131
132
    /**
133
     * Deletes specified configurations (or all if none specified) from the parameters table
134
     * @param array|string $names
135
     * @param integer $tenantId
136
     */
137 1
    public function delete($names = [])
138
    {
139 1
        if (is_array($names)) {
140 1
            $where = ['IN', $this->keyColumnName, $names];
141 1
        } else {
142 1
            $where = [$this->keyColumnName => $names];
143
        }
144
145 1
        $event = new SettingsEvent();
146 1
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
147
148 1
        if ($event->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $event->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
149
            $where = array_merge($event->columns, $where);
150
        }
151
152 1
        $this->getDb()
153 1
            ->createCommand()
154 1
            ->delete($this->tableName, $where)
155 1
            ->execute();
156 1
    }
157
158
    /**
159
     * Creates query to find settings value
160
     * @param string $name
161
     * @return \yii\db\Query
162
     */
163 2
    protected function createQuery($name = null)
164
    {
165 2
        $query = (new Query())
166 2
            ->select([$this->valueColumnName])
167 2
            ->from($this->tableName);
168
169 2
        $event = new SettingsEvent();
170 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
171
172 2
        if ($event->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $event->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
173 1
            $query->andWhere($event->columns);
174 1
        }
175
176 2
        if ($name) {
177 2
            $query->andWhere([$this->keyColumnName => $name]);
178 2
        }
179
180 2
        return $query;
181
    }
182
183
}
184