Completed
Push — develop ( 65300b...355392 )
by Nate
04:47
created

m190326_080222_namespace::safeUp()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 23
cp 0
rs 9.376
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\ArrayHelper;
13
use craft\helpers\Json;
14
use craft\helpers\StringHelper;
15
use craft\records\Field;
16
use flipbox\craft\element\lists\fields\UserList;
17
18
class m190326_080222_namespace extends Migration
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function safeUp()
24
    {
25
        $records = Field::find()
26
            ->andWhere(
27
                [
28
                'type' => "flipbox\\element\\lists\\fields\\UserSourceList"
29
                ]
30
            )
31
            ->all();
32
33
        $success = true;
34
35
        /**
36
 * @var Field $record
37
*/
38
        foreach ($records as $record) {
39
            $record->type = UserList::class;
40
41
            $settings = $record->settings ?? [];
42
            if (is_string($settings)) {
43
                $settings = Json::decodeIfJson($settings);
44
            }
45
46
            // Update settings
47
            $record->settings = $settings;
48
49
            // Save
50
            if (!$record->save()) {
51
                $success = false;
52
            }
53
        }
54
55
        return $success;
56
    }
57
}
58