Completed
Push — master ( 74eec6...73722f )
by Gino
01:25
created

UpdateSettingsAbstract::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 UpdateSettingsAbstract
12
 *
13
 * @package GinoPane\AwesomeIconsList\Updates
14
 */
15
abstract class UpdateSettingsAbstract extends Migration
0 ignored issues
show
Coding Style introduced by
UpdateSettingsAbstract does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
16
{
17
    protected static $PREVIOUS_FONTAWESOME_LINK = '';
0 ignored issues
show
Coding Style introduced by
$PREVIOUS_FONTAWESOME_LINK does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
    protected static $PREVIOUS_FONTAWESOME_LINK_ATTRIBUTES = [];
0 ignored issues
show
Coding Style introduced by
$PREVIOUS_FONTAWESOME_LINK_ATTRIBUTES does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
20
    protected static $NEW_FONTAWESOME_LINK = '';
0 ignored issues
show
Coding Style introduced by
$NEW_FONTAWESOME_LINK does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
21
    protected static $NEW_FONTAWESOME_LINK_ATTRIBUTES = [];
0 ignored issues
show
Coding Style introduced by
$NEW_FONTAWESOME_LINK_ATTRIBUTES does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
22
23
    /**
24
     * Execute migrations
25
     */
26
    public function up()
27
    {
28
        $this->updateOrInsertSettings(static::$NEW_FONTAWESOME_LINK, static::$NEW_FONTAWESOME_LINK_ATTRIBUTES);
29
    }
30
31
    /**
32
     * Rollback migrations
33
     */
34
    public function down()
35
    {
36
        $this->updateOrInsertSettings(static::$PREVIOUS_FONTAWESOME_LINK, static::$PREVIOUS_FONTAWESOME_LINK_ATTRIBUTES);
37
    }
38
39
    /**
40
     * @param string $link
41
     * @param array  $attributes
42
     */
43
    private function updateOrInsertSettings(string $link, array $attributes)
44
    {
45
        if (Schema::hasTable('system_settings')) {
46
            $currentSettings = DB::table('system_settings')->whereItem(Settings::SETTINGS_CODE)->pluck('value')->first();
47
48
            $settings = [];
49
50
            $settings[Settings::FONTAWESOME_LINK_KEY ] = $link;
51
            $settings[Settings::FONTAWESOME_LINK_ATTRIBUTES_KEY] = $attributes;
52
53
            if ($currentSettings !== null) {
54
                DB::table('system_settings')->whereItem(Settings::SETTINGS_CODE)->update(
55
                    ['value' => json_encode($settings)]
56
                );
57
            } else {
58
                DB::table('system_settings')->insert(
59
                    ['item' => Settings::SETTINGS_CODE, 'value' => json_encode($settings)]
60
                );
61
            }
62
        }
63
    }
64
}
65