Completed
Push — master ( 8c95bb...199f9c )
by Nate
15:05
created

ObjectTable::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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