FwkDbTestUtil::dropTestDb()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
$vendorDir = __DIR__ . '/../vendor';
3
4
if (!@include($vendorDir . '/autoload.php')) {
5
    die("You must set up the project dependencies, run the following commands:
6
wget http://getcomposer.org/composer.phar
7
php composer.phar install
8
");
9
}
10
11
class FwkDbTestUtil
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    private static $created = false;
14
15
    public static function createTestDb(\Fwk\Db\Connection $connection)
16
    {
17
        if(self::$created == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
18
19
            return;
20
21
        $schema = $connection->getSchema();
22
        $tbl = $schema->createTable('fwkdb_test_users');
23
        $tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
24
        $tbl->addColumn("username", "string", array("length" => 32));
25
        $tbl->addColumn("phone_id", "integer", array("unsigned" => true, "notnull" => false));
26
        $tbl->addColumn('created_at', 'datetime', array('notnull' => false));
27
        $tbl->addColumn('updated_at', 'datetime', array('notnull' => false));
28
        $tbl->setPrimaryKey(array("id"));
29
        $tbl->addUniqueIndex(array("username"));
30
31
        $tbl = $schema->createTable('fwkdb_test_emails');
32
        $tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
33
        $tbl->addColumn("email", "string", array("length" => 255));
34
        $tbl->addColumn("verified", "integer", array("length" => 1, "unsigned" => true));
35
        $tbl->setPrimaryKey(array("id"));
36
        $tbl->addUniqueIndex(array("email"));
37
38
        $tbl = $schema->createTable('fwkdb_test_users_emails');
39
        $tbl->addColumn("user_id", "integer", array("unsigned" => true));
40
        $tbl->addColumn("email_id", "integer", array("unsigned" => true));
41
        $tbl->setPrimaryKey(array("user_id", "email_id"));
42
43
        $tbl = $schema->createTable('fwkdb_test_users_metas');
44
        $tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
45
        $tbl->addColumn("user_id", "integer", array("unsigned" => true));
46
        $tbl->addColumn("name", "string", array("length" => 50));
47
        $tbl->addColumn("value", "string");
48
        $tbl->addIndex(array('user_id'));
49
        $tbl->setPrimaryKey(array("id"));
50
51
        $tbl = $schema->createTable('fwkdb_test_phones');
52
        $tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
53
        $tbl->addColumn("number", "string", array("length" => 50));
54
        $tbl->setPrimaryKey(array("id"));
55
56
        $connection->connect();
57
        $queries = $schema->toSql($connection->getDriver()->getDatabasePlatform());
58
        foreach ($queries as $query) {
59
            $connection->getDriver()->exec($query);
60
        }
61
62
        self::$created = true;
63
    }
64
65
    public static function dropTestDb(\Fwk\Db\Connection $connection)
66
    {
67
        if(self::$created == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
68
            return;
69
70
        $schema = $connection->getSchema();
71
        $queries = $schema->toDropSql($connection->getDriver()->getDatabasePlatform());
72
        foreach ($queries as $query) {
73
            $connection->getDriver()->exec($query);
74
        }
75
76
        self::$created = false;
77
    }
78
}
79