Passed
Branch dev (4bd34d)
by Bob
02:19
created

updateDramielDB.php ➔ updateDramielDB()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 66
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 1
dl 0
loc 66
rs 8.8291
c 0
b 0
f 0

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
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 * @param $logger
25
 */
26
27
function updateDramielDB($logger)
28
{
29
    $tables = array('storage', 'messageQueue', 'renameQueue', 'corpCache', 'contactList');
30
31
    $tableCreateCode = array(
32
        'storage' => '
33
            BEGIN;
34
            CREATE TABLE IF NOT EXISTS `storage` (
35
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
36
                `key` VARCHAR(255) NOT NULL,
37
                `value` VARCHAR(255) NOT NULL
38
            );
39
            CREATE UNIQUE INDEX key ON storage (key);
40
            COMMIT;',
41
        'messageQueue' => '
42
            BEGIN;
43
            CREATE TABLE IF NOT EXISTS `messageQueue` (
44
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
45
                `message` VARCHAR(255) NOT NULL,
46
                `channel` VARCHAR(255) NOT NULL,
47
                `guild` VARCHAR(255) NOT NULL
48
            );
49
            COMMIT;',
50
        'renameQueue' => '
51
            BEGIN;
52
            CREATE TABLE IF NOT EXISTS `renameQueue` (
53
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
54
                `discordID` VARCHAR(255) NOT NULL,
55
                `nick` VARCHAR(255) NOT NULL,
56
                `guild` VARCHAR(255) NOT NULL
57
            );
58
            COMMIT;',
59
        'corpCache' => '
60
            BEGIN;
61
            CREATE TABLE IF NOT EXISTS `corpCache` (
62
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
63
                `corpID` VARCHAR(255) NOT NULL UNIQUE,
64
                `corpTicker` VARCHAR(255) NOT NULL,
65
                `corpName` VARCHAR(255) NOT NULL
66
            );
67
            COMMIT;',
68
        'contactList' => '
69
            BEGIN;
70
            CREATE TABLE IF NOT EXISTS `contactList` (
71
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
72
                `contactID` VARCHAR(255) NOT NULL UNIQUE,
73
                `contactName` VARCHAR(255) NOT NULL,
74
                `standing` VARCHAR(255) NOT NULL
75
            );
76
            COMMIT;',
77
    );
78
79
    // Does the file exist?
80
    if (!file_exists(__DIR__ . '/../../database/dramiel.sqlite')) {
81
        touch(__DIR__ . '/../../database/dramiel.sqlite');
82
    }
83
84
    // Create table if not exists
85
    foreach ($tables as $table) {
86
        $exists = dbQueryField("SELECT name FROM sqlite_master WHERE type = 'table' AND name = :name", 'name', array(':name' => $table));
87
        if (!$exists) {
88
            $logger->addInfo("Creating {$table} in dramiel.sqlite, since it does not exist");
89
            dbExecute(trim($tableCreateCode[$table]));
90
        }
91
    }
92
}