DbalSchema   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 30 1
A drop() 0 12 3
1
<?php
2
3
namespace Scheduler\Infrastructure\DBAL;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\Schema;
7
8
class DbalSchema
9
{
10
    private $connection;
11
12
    public function __construct(Connection $connection)
13
    {
14
        $this->connection = $connection;
15
    }
16
17
    public function create()
18
    {
19
        $sm = $this->connection->getSchemaManager();
20
        $schema = new Schema();
21
22
        $users = $schema->createTable("users");
23
        $users->addColumn("id", "integer", ["unsigned" => true, "autoincrement" => true]);
24
        $users->addColumn("name", "string", ["length" => 32]);
25
        $users->addColumn("role", "string", ["length" => 8]);
26
        $users->addColumn("email", "string", ["length" => 254, "notnull" => false]);
27
        $users->addColumn("phone", "string", ["length" => 12, "notnull" => false]);
28
        $users->addColumn("created_at", "datetime");
29
        $users->addColumn("updated_at", "datetime");
30
        $users->setPrimaryKey(["id"]);
31
        $sm->createTable($users);
32
33
        $shifts = $schema->createTable("shifts");
34
        $shifts->addColumn("id", "integer", ["unsigned" => true, "autoincrement" => true]);
35
        $shifts->addColumn("manager_id", "integer", ["unsigned" => true]);
36
        $shifts->addColumn("employee_id", "integer", ["unsigned" => true, "notnull" => false]);
37
        $shifts->addColumn("break", "float");
38
        $shifts->addColumn("start_time", "datetime");
39
        $shifts->addColumn("end_time", "datetime");
40
        $shifts->addColumn("created_at", "datetime");
41
        $shifts->addColumn("updated_at", "datetime");
42
        $shifts->setPrimaryKey(["id"]);
43
        $shifts->addForeignKeyConstraint($users, ["manager_id"], ["id"], ["onUpdate" => "CASCADE"]);
44
        $shifts->addForeignKeyConstraint($users, ["employee_id"], ["id"], ["onUpdate" => "CASCADE"]);
45
        $sm->createTable($shifts);
46
    }
47
48
    public function drop()
49
    {
50
        $sm = $this->connection->getSchemaManager();
51
52
        if ($sm->tablesExist(["shifts"])) {
53
            $sm->dropTable("shifts");
54
        }
55
56
        if ($sm->tablesExist(["users"])) {
57
            $sm->dropTable("users");
58
        }
59
    }
60
}
61