Passed
Push — master ( a2c69c...be0438 )
by Tony
10:22
created

SerializeConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 16 4
A down() 0 9 2
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Database\Migrations\Migration;
6
7
class SerializeConfig extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * Run the migrations.
11
     *
12
     * @return void
13
     */
14
    public function up()
15
    {
16
        DB::table('config')->get()->each(function ($config) {
17
            $value = $config->config_value;
18
19
            if (filter_var($value, FILTER_VALIDATE_INT)) {
20
                $value = (int)$value;
21
            } elseif (filter_var($value, FILTER_VALIDATE_FLOAT)) {
22
                $value = (float)$value;
23
            } elseif (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) {
24
                $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
25
            }
26
27
            DB::table('config')
28
                ->where('config_id', $config->config_id)
29
                ->update(['config_value' => json_encode($value)]);
30
        });
31
    }
32
33
    /**
34
     * Reverse the migrations.
35
     *
36
     * @return void
37
     */
38
    public function down()
39
    {
40
        DB::table('config')->get()->each(function ($config) {
41
            $value = json_decode($config->config_value);
42
            $value = is_bool($value) ? var_export($value, true) : (string)$value;
43
44
            DB::table('config')
45
                ->where('config_id', $config->config_id)
46
                ->update(['config_value' => $value]);
47
        });
48
    }
49
}
50