m170324_000003_fix_widget_options   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 18
c 1
b 1
f 0
dl 0
loc 45
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeDown() 0 5 1
A safeUp() 0 28 4
1
<?php
2
3
namespace dukt\analytics\migrations;
4
5
use craft\db\Migration;
6
use craft\db\Query;
7
use craft\helpers\Json;
8
9
/**
10
 * The class name is the UTC timestamp in the format of mYYMMDD_HHMMSS_pluginHandle_migrationName
11
 */
12
class m170324_000003_fix_widget_options extends Migration
13
{
14
    /**
15
     * Any migration code in here is wrapped inside of a transaction.
16
     *
17
     * @return bool
18
     */
19
    public function safeUp()
20
    {
21
        $widgetResults = (new Query())
22
            ->select('*')
23
            ->from(['{{%widgets}}'])
24
            ->where(['type' => 'dukt\analytics\widgets\Realtime'])
25
            ->orWhere(['type' => 'dukt\analytics\widgets\Report'])
26
            ->all();
27
28
        if ($widgetResults) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $widgetResults 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...
29
            foreach ($widgetResults as $result) {
30
                $settings = Json::decode($result['settings']);
31
32
                if (isset($settings['options']['metric'])) {
33
                    $settings['options'] = [
34
                        $settings['chart'] => $settings['options']
35
                    ];
36
                }
37
38
                // Update row
39
40
                $settings = Json::encode($settings);
41
42
                $this->update('{{%widgets}}', ['settings' => $settings], ['id' => $result['id']]);
43
            }
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function safeDown()
53
    {
54
        echo "m170324_000003_fix_widget_options cannot be reverted.\n";
55
56
        return false;
57
    }
58
}
59