Completed
Push — master ( 0a1b5a...13fc78 )
by Edson
01:55
created

Schema   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 86.05%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 81
ccs 37
cts 43
cp 0.8605
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A drop() 0 3 1
A onDelete() 0 5 2
A getColumns() 0 19 3
A __construct() 0 3 1
A create() 0 9 1
A run() 0 3 1
A onUpdate() 0 5 2
A fk() 0 5 2
A pk() 0 5 2
1
<?php
2
3
namespace Bonfim\ActiveRecord;
4
5
abstract class Schema extends ActiveRecord
6
{
7
    private $sql = '';
8
    private $table;
9
10
    abstract public function up();
11
    abstract public function down();
12
13 30
    public function __construct()
14
    {
15 30
        $this->table = new Table();
16 30
    }
17
18 30
    public function create(string $name, $callback)
19
    {
20 30
        call_user_func_array($callback, [$this->table]);
21
22 30
        $q = "CREATE TABLE IF NOT EXISTS $name ({$this->getColumns()}";
23 30
        $q .= $this->pk().$this->fk().$this->onUpdate().$this->onDelete();
24 30
        $q .= "\n) ENGINE=INNODB;";
25
26 30
        $this->sql = $q;
27 30
    }
28
29
    public function drop(string $name)
30
    {
31
        $this->sql = "DROP TABLE $name";
32
    }
33
34 30
    private function pk()
35
    {
36 30
        return (!empty($pk = $this->table->getPk()))
0 ignored issues
show
Bug introduced by
The method getPk() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

36
        return (!empty($pk = $this->table->/** @scrutinizer ignore-call */ getPk()))
Loading history...
37 30
            ? $this->sql .= ",\n\tPRIMARY KEY ($pk)"
38 30
            : "";
39
    }
40
41 30
    private function fk()
42
    {
43 30
        return (!empty($fk = $this->table->getFk()))
0 ignored issues
show
Bug introduced by
The method getFk() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

43
        return (!empty($fk = $this->table->/** @scrutinizer ignore-call */ getFk()))
Loading history...
44
            ? ",\n\t$fk {$this->table->getReferences()}"
0 ignored issues
show
Bug introduced by
The method getReferences() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

44
            ? ",\n\t$fk {$this->table->/** @scrutinizer ignore-call */ getReferences()}"
Loading history...
45 30
            : "";
46
    }
47
48 30
    private function onUpdate()
49
    {
50 30
        return (!empty($on = $this->table->getOnupdate()))
0 ignored issues
show
Bug introduced by
The method getOnupdate() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

50
        return (!empty($on = $this->table->/** @scrutinizer ignore-call */ getOnupdate()))
Loading history...
51
            ? " $on"
52 30
            : "";
53
    }
54
55 30
    private function onDelete()
56
    {
57 30
        return (!empty($on = $this->table->getOndelete()))
0 ignored issues
show
Bug introduced by
The method getOndelete() does not exist on Bonfim\ActiveRecord\Table. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

57
        return (!empty($on = $this->table->/** @scrutinizer ignore-call */ getOndelete()))
Loading history...
58
            ? " $on"
59 30
            : "";
60
    }
61
62 30
    private function getColumns()
63
    {
64 30
        $q = "\n\t";
65
66 30
        $columns = $this->table->getColumns();
67 30
        $count = count($columns);
68
69 30
        reset($columns);
70
71 30
        for ($i = 0; $i < $count; $i++) {
72 30
            $key = key($columns);
73 30
            $q .= key($columns) . ' ' . $columns[$key];
74 30
            if ($i < $count - 1) {
75 30
                $q .= ",\n\t";
76
            }
77 30
            next($columns);
78
        }
79
80 30
        return $q;
81
    }
82
83 30
    public function run()
84
    {
85 30
        self::exec($this->sql);
86 30
    }
87
}
88