Passed
Push — develop ( 542723...1a75a9 )
by M. Mikkel
04:36 queued 45s
created

Install::addForeignKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 48
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 60
rs 9.1344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Cache Flag plugin for Craft CMS 3.x
4
 *
5
 * Flag and clear template caches.
6
 *
7
 * @link      https://vaersaagod.no
8
 * @copyright Copyright (c) 2018 Mats Mikkel Rummelhoff
9
 */
10
11
namespace mmikkel\cacheflag\migrations;
12
13
use mmikkel\cacheflag\CacheFlag;
14
15
use Craft;
16
use craft\config\DbConfig;
17
use craft\db\Migration;
18
19
/**
20
 * @author    Mats Mikkel Rummelhoff
21
 * @package   CacheFlag
22
 * @since     1.0.0
23
 */
24
class Install extends Migration
25
{
26
    // Public Properties
27
    // =========================================================================
28
29
    /**
30
     * @var string The database driver to use
31
     */
32
    public $driver;
33
34
    // Public Methods
35
    // =========================================================================
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function safeUp()
41
    {
42
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
43
        if ($this->createTables()) {
44
            $this->createIndexes();
45
            $this->addForeignKeys();
46
            // Refresh the db schema caches
47
            Craft::$app->db->schema->refresh();
48
            $this->insertDefaultData();
49
        }
50
51
        return true;
52
    }
53
54
   /**
55
     * @inheritdoc
56
     */
57
    public function safeDown()
58
    {
59
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
60
        $this->removeTables();
61
62
        return true;
63
    }
64
65
    // Protected Methods
66
    // =========================================================================
67
68
    /**
69
     * @return bool
70
     */
71
    protected function createTables()
72
    {
73
        $tablesCreated = false;
74
75
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%cacheflag_flags}}');
76
        if ($tableSchema === null) {
77
            $tablesCreated = true;
78
            $this->createTable(
79
                '{{%cacheflag_flags}}',
80
                [
81
                    'id' => $this->primaryKey(),
82
                    'flags' => $this->string(255)->notNull(),
83
                    'sectionId' => $this->integer()->unique(),
84
                    'categoryGroupId' => $this->integer()->unique(),
85
                    'tagGroupId' => $this->integer()->unique(),
86
                    'userGroupId' => $this->integer()->unique(),
87
                    'volumeId' => $this->integer()->unique(),
88
                    'globalSetId' => $this->integer()->unique(),
89
                    'elementType' => $this->string(255)->unique(),
90
                ]
91
            );
92
        }
93
94
        return $tablesCreated;
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    protected function createIndexes()
101
    {
102
        $this->createIndex(
103
            $this->db->getIndexName(
104
                '{{%cacheflag_flags}}',
105
                'flags',
106
                false
107
            ),
108
            '{{%cacheflag_flags}}',
109
            'flags',
110
            false
111
        );
112
        // Additional commands depending on the db driver
113
        switch ($this->driver) {
114
            case DbConfig::DRIVER_MYSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_MYSQL has been deprecated: in 3.4.0. Use [[Connection::DRIVER_MYSQL]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_MYSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
115
                break;
116
            case DbConfig::DRIVER_PGSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_PGSQL has been deprecated: in 3.4.0. Use [[Connection::DRIVER_PGSQL]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_PGSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
117
                break;
118
        }
119
    }
120
121
    /**
122
     * @return void
123
     */
124
    protected function addForeignKeys()
125
    {
126
        $this->addForeignKey(
127
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'sectionId'),
128
            '{{%cacheflag_flags}}',
129
            'sectionId',
130
            '{{%sections}}',
131
            'id',
132
            'CASCADE',
133
            'CASCADE'
134
        );
135
136
        $this->addForeignKey(
137
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'categoryGroupId'),
138
            '{{%cacheflag_flags}}',
139
            'categoryGroupId',
140
            '{{%categorygroups}}',
141
            'id',
142
            'CASCADE',
143
            'CASCADE'
144
        );
145
146
        $this->addForeignKey(
147
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'tagGroupId'),
148
            '{{%cacheflag_flags}}',
149
            'tagGroupId',
150
            '{{%taggroups}}',
151
            'id',
152
            'CASCADE',
153
            'CASCADE'
154
        );
155
156
        $this->addForeignKey(
157
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'userGroupId'),
158
            '{{%cacheflag_flags}}',
159
            'userGroupId',
160
            '{{%usergroups}}',
161
            'id',
162
            'CASCADE',
163
            'CASCADE'
164
        );
165
166
        $this->addForeignKey(
167
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'volumeId'),
168
            '{{%cacheflag_flags}}',
169
            'volumeId',
170
            '{{%volumes}}',
171
            'id',
172
            'CASCADE',
173
            'CASCADE'
174
        );
175
176
        $this->addForeignKey(
177
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'globalSetId'),
178
            '{{%cacheflag_flags}}',
179
            'globalSetId',
180
            '{{%globalsets}}',
181
            'id',
182
            'CASCADE',
183
            'CASCADE'
184
        );
185
    }
186
187
    /**
188
     * @return void
189
     */
190
    protected function insertDefaultData()
191
    {
192
    }
193
194
    /**
195
     * @return void
196
     */
197
    protected function removeTables()
198
    {
199
        $this->dropTableIfExists('{{%cacheflag_flagged}}');
200
201
        $this->dropTableIfExists('{{%cacheflag_flags}}');
202
    }
203
}
204