CreatePostTypeTable::down()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Updates;
4
5
use DB;
6
use Schema;
7
use Carbon\Carbon;
8
use System\Classes\PluginManager;
9
use GinoPane\BlogTaxonomy\Models\PostType;
10
use October\Rain\Database\Updates\Migration;
11
12
/**
13
 * Class CreatePostTypeTable
14
 *
15
 * @package GinoPane\BlogTaxonomy\Updates
16
 */
17
class CreatePostTypeTable extends Migration
18
{
19
    const GAME_REVIEW_TYPE = [
20
        [
21
            'name' => 'Release Date',
22
            'code' => 'release-date',
23
            'type' => 'datepicker',
24
            'datepicker_mode' => 'date'
25
        ],
26
        [
27
            'name' => 'Publisher',
28
            'code' => 'publisher',
29
            'type' => 'text'
30
        ],
31
        [
32
            'name' => 'Editor',
33
            'code' => 'editor',
34
            'type' => 'text'
35
        ],
36
        [
37
            'name' => 'Rating',
38
            'code' => 'rating',
39
            'type' => 'dropdown',
40
            'dropdown_options' => '1,2,3,4,5,6,7,8,9,10',
41
        ],
42
        [
43
            'name' => 'Pros',
44
            'code' => 'pros',
45
            'type' => 'textarea'
46
        ],
47
        [
48
            'name' => 'Cons',
49
            'code' => 'cons',
50
            'type' => 'textarea'
51
        ],
52
    ];
53
54
    /**
55
     * Execute migrations
56
     */
57
    public function up()
58
    {
59
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
60
            $this->createPostTypes();
61
        }
62
    }
63
64
    /**
65
     * Rollback migrations
66
     */
67
    public function down()
68
    {
69
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
70
            $this->dropPostTypes();
71
        }
72
    }
73
74
    /**
75
     * Rollback PostType migration
76
     */
77
    private function dropPostTypes()
78
    {
79
        if (Schema::hasColumn('rainlab_blog_posts', PostType::TABLE_NAME . '_id')) {
80
            Schema::table('rainlab_blog_posts', static function ($table) {
81
                $table->dropForeign([PostType::TABLE_NAME . '_id']);
82
83
                $table->dropColumn(PostType::TABLE_NAME . '_id');
84
            });
85
        }
86
87 View Code Duplication
        if (Schema::hasColumn('rainlab_blog_posts', PostType::TABLE_NAME . '_attributes')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            Schema::table('rainlab_blog_posts', static function ($table) {
89
                $table->dropColumn(PostType::TABLE_NAME . '_attributes');
90
            });
91
        }
92
93
        Schema::dropIfExists(PostType::TABLE_NAME);
94
    }
95
96
    /**
97
     * Create PostType table
98
     */
99
    private function createPostTypes()
100
    {
101
        if (!Schema::hasTable(PostType::TABLE_NAME)) {
102
            Schema::create(
103
                PostType::TABLE_NAME,
104
                static function ($table) {
105
                    $table->engine = 'InnoDB';
106
107
                    $table->increments('id');
108
                    $table->string('name')->unique();
109
                    $table->string('slug')->unique();
110
                    $table->text('description')->nullable();
111
                    $table->text('type_attributes')->nullable();
112
                    $table->timestamps();
113
                }
114
            );
115
116 View Code Duplication
            Schema::table('rainlab_blog_posts', function ($table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
                $table->integer(PostType::TABLE_NAME . '_id')->unsigned()->nullable()->default(null);
118
                $table->foreign(PostType::TABLE_NAME . '_id')->references('id')->on(PostType::TABLE_NAME)->onDelete('cascade');
119
120
                $table->text(PostType::TABLE_NAME. '_attributes')->nullable();
121
            });
122
123
            DB::table(PostType::TABLE_NAME)->insert(
124
                [
125
                    'name' => 'Game Review',
126
                    'slug' => 'game-review',
127
                    'type_attributes' => json_encode(self::GAME_REVIEW_TYPE),
128
                    'created_at' => Carbon::now(),
129
                    'updated_at' => Carbon::now(),
130
                ]
131
            );
132
        }
133
    }
134
}
135