safeUp()   C
last analyzed

Complexity

Conditions 14
Paths 2

Size

Total Lines 110
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 80
nc 2
nop 0
dl 0
loc 110
rs 5.4496
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 m141009_105954_analytics_reportsWidgetToExplorerWidget 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' => 'Analytics_Reports'])
25
            ->all();
26
27
        if (!empty($widgetResults)) {
28
29
            foreach ($widgetResults as $result) {
30
                $settings = Json::decode($result['settings']);
31
                $newSettings = [];
32
33
                if (!empty($settings['type'])) {
34
                    switch ($settings['type']) {
35
                        case 'visits':
36
                            $newSettings = [
37
                                'menu' => 'audienceOverview',
38
                                'dimension' => '',
39
                                'metric' => 'ga:sessions',
40
                                'chart' => 'area',
41
                                'period' => 'month',
42
                            ];
43
                            break;
44
45
                        case 'geo':
46
                            $newSettings = [
47
                                'menu' => 'location',
48
                                'dimension' => 'ga:country',
49
                                'metric' => 'ga:pageviewsPerSession',
50
                                'chart' => 'geo',
51
                                'period' => 'month',
52
                            ];
53
                            break;
54
55
                        case 'mobile':
56
                            $newSettings = [
57
                                'menu' => 'mobile',
58
                                'dimension' => 'ga:deviceCategory',
59
                                'metric' => 'ga:sessions',
60
                                'chart' => 'pie',
61
                                'period' => 'week',
62
                            ];
63
                            break;
64
65
                        case 'pages':
66
                            $newSettings = [
67
                                'menu' => 'allPages',
68
                                'dimension' => 'ga:pagePath',
69
                                'metric' => 'ga:pageviews',
70
                                'chart' => 'table',
71
                                'period' => 'week',
72
                            ];
73
                            break;
74
75
                        case 'acquisition':
76
                            $newSettings = [
77
                                'menu' => 'allChannels',
78
                                'dimension' => 'ga:channelGrouping',
79
                                'metric' => 'ga:sessions',
80
                                'chart' => 'table',
81
                                'period' => 'week',
82
                            ];
83
                            break;
84
85
                        case 'technology':
86
                            $newSettings = [
87
                                'menu' => 'browserOs',
88
                                'dimension' => 'ga:browser',
89
                                'metric' => 'ga:sessions',
90
                                'chart' => 'pie',
91
                                'period' => 'week',
92
                            ];
93
                            break;
94
95
                        case 'conversions':
96
                            $newSettings = [
97
                                'menu' => 'goals',
98
                                'dimension' => 'ga:goalCompletionLocation',
99
                                'metric' => 'ga:goalCompletionsAll',
100
                                'chart' => 'area',
101
                                'period' => 'week',
102
                            ];
103
                            break;
104
105
                        case 'counts':
106
                        case 'custom':
107
                        case 'realtime':
108
                            $newSettings = [
109
                                'menu' => 'audienceOverview',
110
                                'dimension' => '',
111
                                'metric' => 'ga:sessions',
112
                                'chart' => 'area',
113
                                'period' => 'month',
114
                            ];
115
                            break;
116
                    }
117
118
119
                    // Update rows
120
121
                    $newSettings = Json::encode($newSettings);
122
123
                    $this->update('{{%widgets}}', ['type' => 'Analytics_Explorer', 'settings' => $newSettings], ['id' => $result['id']]);
124
                }
125
            }
126
        }
127
128
        return true;
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function safeDown()
135
    {
136
        echo "m141009_105954_analytics_reportsWidgetToExplorerWidget cannot be reverted.\n";
137
138
        return false;
139
    }
140
}
141