IntegrationAssociations::addForeignKeys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 30
cp 0
rs 9.44
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Element as ElementRecord;
13
use craft\records\Field as FieldRecord;
14
use craft\records\Site as SiteRecord;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
abstract class IntegrationAssociations extends Migration
21
{
22
    /**
23
     * @return string
24
     */
25
    abstract protected static function tableName(): string;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function safeUp()
31
    {
32
        $this->createTables();
33
        $this->createIndexes();
34
        $this->addForeignKeys();
35
36
        return true;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function safeDown()
43
    {
44
        $this->dropTableIfExists(static::tableName());
45
        return true;
46
    }
47
48
    /**
49
     * Creates the tables.
50
     *
51
     * @return void
52
     */
53
    protected function createTables()
54
    {
55
        $this->createTable(static::tableName(), [
56
            'objectId' => $this->string()->notNull(),
57
            'elementId' => $this->integer()->notNull(),
58
            'fieldId' => $this->integer()->notNull(),
59
            'siteId' => $this->integer()->notNull(),
60
            'sortOrder' => $this->smallInteger()->unsigned(),
61
            'dateCreated' => $this->dateTime()->notNull(),
62
            'dateUpdated' => $this->dateTime()->notNull(),
63
            'uid' => $this->uid(),
64
        ]);
65
    }
66
67
    /**
68
     * Creates the indexes.
69
     *
70
     * @return void
71
     */
72
    protected function createIndexes()
73
    {
74
        $this->addPrimaryKey(
75
            null,
76
            static::tableName(),
77
            [
78
                'elementId',
79
                'objectId',
80
                'fieldId',
81
                'siteId'
82
            ]
83
        );
84
        $this->createIndex(
85
            null,
86
            static::tableName(),
87
            'objectId',
88
            false
89
        );
90
    }
91
92
    /**
93
     * Adds the foreign keys.
94
     *
95
     * @return void
96
     */
97
    protected function addForeignKeys()
98
    {
99
        $this->addForeignKey(
100
            null,
101
            static::tableName(),
102
            'elementId',
103
            ElementRecord::tableName(),
104
            'id',
105
            'CASCADE',
106
            null
107
        );
108
        $this->addForeignKey(
109
            null,
110
            static::tableName(),
111
            'siteId',
112
            SiteRecord::tableName(),
113
            'id',
114
            'CASCADE',
115
            'CASCADE'
116
        );
117
        $this->addForeignKey(
118
            null,
119
            static::tableName(),
120
            'fieldId',
121
            FieldRecord::tableName(),
122
            'id',
123
            'CASCADE',
124
            'CASCADE'
125
        );
126
    }
127
}
128