Completed
Push — master ( 5e2abd...c05db9 )
by Leandro
01:20
created

Settings   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.81%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 6
dl 0
loc 184
ccs 73
cts 77
cp 0.9481
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDb() 0 4 1
A exists() 0 5 1
A beforeExecute() 0 6 1
A get() 0 14 5
A set() 0 24 3
A all() 0 15 2
A save() 0 6 2
A remove() 0 18 3
A createQuery() 0 18 3
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
0 ignored issues
show
Bug introduced by leandrogehlen
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 leandrogehlen
$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 = $this->beforeExecute();
85 2
        if ($event->data) {
86 1
            $values = array_merge($event->data, $values);
87 1
            $where = array_merge($event->data, $where);
88 1
        }
89
90 2
        if ($this->exists($name)) {
91 1
            $db->createCommand()
92 1
                ->update($this->tableName, $values, $where)
93 1
                ->execute();
94 1
        } 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
0 ignored issues
show
Bug introduced by leandrogehlen
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...
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 1
        }
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 1
        }
133 1
    }
134
135
    /**
136
     * Deletes specified configurations (or all if none specified) from the parameters table
137
     * @param array|string $names
138
     * @param integer $tenantId
0 ignored issues
show
Bug introduced by leandrogehlen
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...
139
     */
140 1
    public function remove($names = [])
141
    {
142 1
        if (is_array($names)) {
143 1
            $where = ['IN', $this->keyColumnName, $names];
144 1
        } else {
145 1
            $where = [$this->keyColumnName => $names];
146
        }
147
148 1
        $event = $this->beforeExecute();
149 1
        if ($event->data) {
150
            $where = array_merge($event->data, $where);
151
        }
152
153 1
        $this->getDb()
154 1
            ->createCommand()
155 1
            ->delete($this->tableName, $where)
156 1
            ->execute();
157 1
    }
158
159
    /**
160
     * Creates query to find settings value
161
     * @param string $name
162
     * @return \yii\db\Query
163
     */
164 2
    protected function createQuery($name = null)
165
    {
166 2
        $query = (new Query())
167 2
            ->select([$this->valueColumnName])
168 2
            ->from($this->tableName);
169
170 2
        $event = $this->beforeExecute();
171
172 2
        if ($event->data) {
173 1
            $query->andWhere($event->data);
174 1
        }
175
176 2
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by leandrogehlen
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...
177 2
            $query->andWhere([$this->keyColumnName => $name]);
178 2
        }
179
180 2
        return $query;
181
    }
182
183
    /**
184
     * This method is called at the before execute db command
185
     * @return yii\base\Event
186
     */
187 2
    protected function beforeExecute()
188
    {
189 2
        $event = new Event();
190 2
        $this->trigger(self::EVENT_BEFORE_EXECUTE, $event);
191 2
        return $event;
192
    }
193
194
}
195