CreatePolymorphicTagTable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 76
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 dropTaggablesTable() 0 4 1
A createTaggablesTable() 0 42 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Updates;
4
5
use Schema;
6
use RainLab\Blog\Models\Post;
7
use System\Classes\PluginManager;
8
use Illuminate\Support\Facades\DB;
9
use GinoPane\BlogTaxonomy\Models\Tag;
10
use October\Rain\Database\Updates\Migration;
11
12
/**
13
 * Class CreatePolymorphicTagTable
14
 *
15
 * @package GinoPane\BlogTaxonomy\Updates
16
 */
17
class CreatePolymorphicTagTable extends Migration
18
{
19
    /**
20
     * Execute migrations
21
     */
22
    public function up()
23
    {
24
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
25
            $this->createTaggablesTable();
26
        }
27
    }
28
29
    /**
30
     * Rollback migrations
31
     */
32
    public function down()
33
    {
34
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
35
            $this->dropTaggablesTable();
36
        }
37
    }
38
39
    /**
40
     * Rollback Taggables migration
41
     */
42
    private function dropTaggablesTable()
43
    {
44
        Schema::dropIfExists(Tag::PIVOT_TABLE);
45
    }
46
47
    /**
48
     * Create Taggables table
49
     */
50
    private function createTaggablesTable()
51
    {
52
        if (!Schema::hasTable(Tag::PIVOT_TABLE)) {
53
            $pivotColumnId = Tag::PIVOT_COLUMN . '_id';
54
            $pivotColumnType = Tag::PIVOT_COLUMN . '_type';
55
56
            Schema::create(
57
                Tag::PIVOT_TABLE,
58
                static function ($table) use ($pivotColumnId, $pivotColumnType) {
59
                    $table->engine = 'InnoDB';
60
61
                    $table->integer('tag_id')->unsigned()->nullable()->default(null);
62
                    $table->integer($pivotColumnId)->unsigned()->nullable()->default(null);
63
                    $table->string($pivotColumnType);
64
65
                    $table->index(
66
                        ['tag_id', Tag::PIVOT_COLUMN . '_id', Tag::PIVOT_COLUMN . '_type'],
67
                        'ginopane_blogtaxonomy_taggable_index'
68
                    );
69
70
                    $table
71
                        ->foreign('tag_id')
72
                        ->references('id')
73
                        ->on(Tag::TABLE_NAME)
74
                        ->onDelete('cascade');
75
                }
76
            );
77
78
            // Current tag relations
79
            $savedTags = DB::table('ginopane_blogtaxonomy_post_tag')->select('tag_id', 'post_id')->get()->toArray();
80
81
            $savedTags = array_map(static function($savedTag) use ($pivotColumnId, $pivotColumnType) {
82
                return [
83
                    'tag_id' => $savedTag->tag_id,
84
                    $pivotColumnId => $savedTag->post_id,
85
                    $pivotColumnType => Post::class
86
                ];
87
            }, $savedTags);
88
89
            DB::table(Tag::PIVOT_TABLE)->insert($savedTags);
90
        }
91
    }
92
}
93