m190326_080222_namespace::safeUp()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 21
cp 0
rs 9.44
c 0
b 0
f 0
cc 4
nc 5
nop 0
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\migrations;
10
11
use craft\db\Migration;
12
use craft\helpers\Json;
13
use craft\records\Field;
14
use flipbox\craft\element\lists\fields\UserList;
15
16
class m190326_080222_namespace extends Migration
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function safeUp()
22
    {
23
        $records = Field::find()
24
            ->andWhere([
25
                'type' => "flipbox\\element\\lists\\fields\\UserSourceList"
26
            ])
27
            ->all();
28
29
        $success = true;
30
31
        /** @var Field $record */
32
        foreach ($records as $record) {
33
            $record->type = UserList::class;
34
35
            $settings = $record->settings ?? [];
36
            if (is_string($settings)) {
37
                $settings = Json::decodeIfJson($settings);
38
            }
39
40
            // Update settings
41
            $record->settings = $settings;
42
43
            // Save
44
            if (!$record->save()) {
45
                $success = false;
46
            }
47
        }
48
49
        return $success;
50
    }
51
}
52