m190725_110548_setting_init::safeUp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 70
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 70
rs 9.2
cc 2
nc 2
nop 0

How to fix   Long Method   

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
use App\Db\Migration;
3
use function GuzzleHttp\json_encode;
4
5
/**
6
 * Class m190725_110548_setting_init
7
 */
8
class m190725_110548_setting_init extends Migration
9
{
10
    private $tableName = '{{%setting}}';
11
    
12
    public function safeUp()
13
    {
14
        $now = time();
15
        $columns = ['id', 'description', 'value', 'rules', 'create_time', 'update_time'];
16
        $items = [
17
            [
18
                'id' => 'upload.image.directory',
19
                'description' => 'Image Directory',
20
                'value' => 'images',
21
                'rules' => [
22
                    ['required'],
23
                ],
24
            ],
25
            [
26
                'id' => 'upload.image.maxSize',
27
                'description' => 'Image Maximum Size(bytes)',
28
                'value' => 1024 * 1024,
29
                'rules' => [
30
                    ['required'],
31
                    ['number'],
32
                ],
33
            ],
34
            [
35
                'id' => 'upload.image.extensions',
36
                'description' => 'Image Extensions',
37
                'value' => 'gif,jpeg,jpg,png',
38
                'rules' => [
39
                    ['required'],
40
                ],
41
            ],
42
43
            [
44
                'id' => 'upload.video.directory',
45
                'description' => 'Video Directory',
46
                'value' => 'videos',
47
                'rules' => [
48
                    ['required'],
49
                ],
50
            ],
51
            [
52
                'id' => 'upload.video.maxSize',
53
                'description' => 'Video Maximum Size(bytes)',
54
                'value' => 10 * 1024 * 1024,
55
                'rules' => [
56
                    ['required'],
57
                    ['number'],
58
                ],
59
            ],
60
            [
61
                'id' => 'upload.video.extensions',
62
                'description' => 'Video Extensions',
63
                'value' => 'mp4',
64
                'rules' => [
65
                    ['required'],
66
                ],
67
            ],
68
        ];
69
70
        $rows = [];
71
        foreach ($items as $item) {
72
            $rows[] = [
73
                $item['id'],
74
                $item['description'],
75
                $item['value'],
76
                json_encode($item['rules'] ?? []),
77
                $now,
78
                $now
79
            ];
80
        }
81
        $this->batchInsert($this->tableName, $columns, $rows);
82
    }
83
84
    public function safeDown()
85
    {
86
        $this->delete($this->tableName);
87
    }
88
}
89