Completed
Push — master ( 1e391f...74eec6 )
by Gino
01:25
created

UpdateSettingsTo570::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GinoPane\AwesomeIconsList\Updates;
4
5
use DB;
6
use Schema;
7
use October\Rain\Database\Updates\Migration;
8
use GinoPane\AwesomeIconsList\Models\Settings;
9
10
/**
11
 * Class UpdateSettingsTo570
12
 *
13
 * @package GinoPane\AwesomeIconsList\Updates
14
 */
15
class UpdateSettingsTo570 extends Migration
16
{
17
    const PREVIOUS_FONTAWESOME_LINK = "https://use.fontawesome.com/releases/v5.6.3/css/all.css";
18
    const PREVIOUS_FONTAWESOME_LINK_ATTRIBUTES = [
19
        [
20
            'attribute' => 'integrity',
21
            'value'     => 'sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/'
22
        ],
23
        [
24
            'attribute' => 'crossorigin',
25
            'value'     => 'anonymous'
26
        ]
27
    ];
28
29
    const NEW_FONTAWESOME_LINK = 'https://use.fontawesome.com/releases/v5.7.0/css/all.css';
30
    const NEW_FONTAWESOME_LINK_ATTRIBUTES = [
31
        [
32
            'attribute' => 'integrity',
33
            'value'     => 'sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ'
34
        ],
35
        [
36
            'attribute' => 'crossorigin',
37
            'value'     => 'anonymous'
38
        ]
39
    ];
40
41
    /**
42
     * Execute migrations
43
     */
44
    public function up()
45
    {
46
        $this->updateOrInsertSettings(self::NEW_FONTAWESOME_LINK, self::NEW_FONTAWESOME_LINK_ATTRIBUTES);
47
    }
48
49
    /**
50
     * Rollback migrations
51
     */
52
    public function down()
53
    {
54
        $this->updateOrInsertSettings(self::PREVIOUS_FONTAWESOME_LINK, self::PREVIOUS_FONTAWESOME_LINK_ATTRIBUTES);
55
    }
56
57
    /**
58
     * @param string $link
59
     * @param array  $attributes
60
     */
61
    private function updateOrInsertSettings(string $link, array $attributes)
62
    {
63
        if (Schema::hasTable('system_settings')) {
64
            $currentSettings = DB::table('system_settings')->whereItem(Settings::SETTINGS_CODE)->pluck('value')->first();
65
66
            $settings[Settings::FONTAWESOME_LINK_KEY ] = $link;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $settings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
            $settings[Settings::FONTAWESOME_LINK_ATTRIBUTES_KEY] = $attributes;
68
69
            if ($currentSettings !== null) {
70
                DB::table('system_settings')->whereItem(Settings::SETTINGS_CODE)->update(
71
                    ['value' => json_encode($settings)]
72
                );
73
            } else {
74
                DB::table('system_settings')->insert(
75
                    ['item' => Settings::SETTINGS_CODE, 'value' => json_encode($settings)]
76
                );
77
            }
78
        }
79
    }
80
}