CreateRelatedSeriesTable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 61
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 dropRelation() 0 4 1
A createRelation() 0 27 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Updates;
4
5
use Schema;
6
use System\Classes\PluginManager;
7
use GinoPane\BlogTaxonomy\Models\Series;
8
use October\Rain\Database\Updates\Migration;
9
10
/**
11
 * Class CreateRelatedSeriesTable
12
 *
13
 * @package GinoPane\BlogTaxonomy\Updates
14
 */
15
class CreateRelatedSeriesTable extends Migration
16
{
17
    /**
18
     * Execute migrations
19
     */
20
    public function up()
21
    {
22
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
23
            $this->createRelation();
24
        }
25
    }
26
27
    /**
28
     * Rollback migrations
29
     */
30
    public function down()
31
    {
32
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
33
            $this->dropRelation();
34
        }
35
    }
36
37
    /**
38
     * Rollback relation migration
39
     */
40
    private function dropRelation()
41
    {
42
        Schema::dropIfExists(Series::RELATED_SERIES_TABLE_NAME);
43
    }
44
45
    /**
46
     * Create relation table
47
     */
48
    private function createRelation()
49
    {
50
        if (!Schema::hasTable(Series::RELATED_SERIES_TABLE_NAME)) {
51
            Schema::create(
52
                Series::RELATED_SERIES_TABLE_NAME,
53
                static function ($table) {
54
                    $table->engine = 'InnoDB';
55
56
                    $table->integer('series_id')->unsigned();
57
                    $table->integer('related_series_id')->unsigned();
58
                    $table->index(['series_id', 'related_series_id'], 'related_series_index');
59
60
                    $table
61
                        ->foreign('series_id', 'Series reference')
62
                        ->references('id')
63
                        ->on(Series::TABLE_NAME)
64
                        ->onDelete('cascade');
65
66
                    $table
67
                        ->foreign('related_series_id', 'Related series reference')
68
                        ->references('id')
69
                        ->on(Series::TABLE_NAME)
70
                        ->onDelete('cascade');
71
                }
72
            );
73
        }
74
    }
75
}
76