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

Settings::delete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.004
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 = '{{%settings}}';
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
0 ignored issues
show
Bug introduced by
There is no parameter named $tenantId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 = new Event();
85 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
86
87 2
        if ($event->data) {
88 1
            $values = array_merge($event->data, $values);
89 1
            $where = array_merge($event->data, $where);
90
        }
91
92 2
        if ($this->exists($name)) {
93 1
            $db->createCommand()
94 1
                ->update($this->tableName, $values, $where)
95 1
                ->execute();
96
        } else  {
97 2
            $values = array_merge($values, $where);
98
99 2
            $db->createCommand()
100 2
                ->insert($this->tableName, $values)
101 2
                ->execute();
102
        }
103 2
    }
104
105
    /**
106
     * Retrieves all configurations stored in database
107
     * @param integer $tenantId
0 ignored issues
show
Bug introduced by
There is no parameter named $tenantId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
108
     * @return array
109
     */
110 1
    public function all()
111
    {
112 1
        $result = [];
113
114 1
        $query = $this->createQuery()
115 1
            ->addSelect($this->keyColumnName);
116
117 1
        $rows = $query->all($this->getDb());
118
119 1
        foreach ($rows as $row) {
120 1
            $result[$row[$this->keyColumnName]] = $row[$this->valueColumnName];
121
        }
122
123 1
        return $result;
124
    }
125
126
    /**
127
     * Store all configuration in database
128
     * @param array $names
129
     */
130 1
    public function save($names)
131
    {
132 1
        foreach ($names as $key => $value) {
133 1
            $this->set($key, $value) ;
134
        }
135 1
    }
136
137
    /**
138
     * Deletes specified configurations (or all if none specified) from the parameters table
139
     * @param array|string $names
140
     * @param integer $tenantId
0 ignored issues
show
Bug introduced by
There is no parameter named $tenantId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
141
     */
142 1
    public function delete($names = [])
143
    {
144 1
        if (is_array($names)) {
145 1
            $where = ['IN', $this->keyColumnName, $names];
146
        } else {
147 1
            $where = [$this->keyColumnName => $names];
148
        }
149
150 1
        $event = new Event();
151 1
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
152
153 1
        if ($event->data) {
154
            $where = array_merge($event->data, $where);
155
        }
156
157 1
        $this->getDb()
158 1
            ->createCommand()
159 1
            ->delete($this->tableName, $where)
160 1
            ->execute();
161 1
    }
162
163
    /**
164
     * Creates query to find settings value
165
     * @param string $name
166
     * @return \yii\db\Query
167
     */
168 2
    protected function createQuery($name = null)
169
    {
170 2
        $query = (new Query())
171 2
            ->select([$this->valueColumnName])
172 2
            ->from($this->tableName);
173
174 2
        $event = new Event();
175 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
176
177 2
        if ($event->data) {
178 1
            $query->andWhere($event->data);
179
        }
180
181 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...
182 2
            $query->andWhere([$this->keyColumnName => $name]);
183
        }
184
185 2
        return $query;
186
    }
187
188
}
189