Completed
Push — master ( c9b1f2...7edc4b )
by Gino
02:53 queued 01:13
created

CreatePostTypeTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 18.97 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 22
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 6 2
A down() 0 6 2
A dropPostTypes() 16 16 2
A createPostTypes() 6 35 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    private function dropPostTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        Schema::table('rainlab_blog_posts', static function ($table) {
80
            $table->dropForeign([PostType::TABLE_NAME . '_id']);
81
82
            $table->dropColumn(PostType::TABLE_NAME. '_attributes');
83
        });
84
85
        if (Schema::hasColumn('rainlab_blog_posts', PostType::TABLE_NAME . '_id')) {
86
            Schema::table('rainlab_blog_posts', static function ($table) {
87
                $table->dropColumn(PostType::TABLE_NAME . '_id');
88
            });
89
        }
90
91
        Schema::dropIfExists(PostType::TABLE_NAME);
92
    }
93
94
    /**
95
     * Create PostType table
96
     */
97
    private function createPostTypes()
98
    {
99
        if (!Schema::hasTable(PostType::TABLE_NAME)) {
100
            Schema::create(
101
                PostType::TABLE_NAME,
102
                static function ($table) {
103
                    $table->engine = 'InnoDB';
104
105
                    $table->increments('id');
106
                    $table->string('name')->unique();
107
                    $table->string('slug')->unique();
108
                    $table->text('description')->nullable();
109
                    $table->json('type_attributes')->nullable();
110
                    $table->timestamps();
111
                }
112
            );
113
114 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...
115
                $table->integer(PostType::TABLE_NAME . '_id')->unsigned()->nullable()->default(null);
116
                $table->foreign(PostType::TABLE_NAME . '_id')->references('id')->on(PostType::TABLE_NAME)->onDelete('cascade');
117
118
                $table->json(PostType::TABLE_NAME. '_attributes')->nullable();
119
            });
120
121
            DB::table(PostType::TABLE_NAME)->insert(
122
                [
123
                    'name' => 'Game Review',
124
                    'slug' => 'game-review',
125
                    'type_attributes' => json_encode(self::GAME_REVIEW_TYPE),
126
                    'created_at' => Carbon::now(),
127
                    'updated_at' => Carbon::now(),
128
                ]
129
            );
130
        }
131
    }
132
}
133