Passed
Push — main ( 3f5d4e...df4575 )
by James Ekow Abaka
01:53
created

Column::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.7998
1
<?php
2
namespace yentu\database;
3
4
use yentu\Parameters;
5
6
class Column extends DatabaseItem implements Commitable
7
{
8
    private $type;
9
    private $table;
10
    private $length;
11
12
    // Description items
13
    private $nulls;
14
    private $name;
15
    private $default;
16
    
17
    #[\Override]
18
    protected function buildDescription()
19
    {
20
        return array(
21
            'name' => $this->name,
22
            'type' => $this->type,
23
            'table' => $this->table->getName(),
24
            'schema' => $this->table->getSchema()->getName(),
25
            'nulls' => $this->nulls,
26
            'length' => $this->length,
27
            'default' => $this->default
28
        );
29
    }
30
    
31
    public function __construct($name, $table)
32
    {
33
        $this->table = $table;
34
        $this->name = $name;
35
    }
36
    
37
    #[\Override]
38
    public function init()
39
    {
40
        $column = Parameters::wrap($this->getDriver()->doesColumnExist(
0 ignored issues
show
Bug introduced by
The method doesColumnExist() does not exist on yentu\ChangeLogger. 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

40
        $column = Parameters::wrap($this->getDriver()->/** @scrutinizer ignore-call */ doesColumnExist(
Loading history...
41
                array(
42
                    'table' => $this->table->getName(),
43
                    'schema' => $this->table->getSchema()->getName(),
44
                    'name' => $this->name
45
                )
46
            ),
47
            ['default', 'length', 'nulls', 'type']
48
        );
49
        if($column === false) {
50
            $this->new = true;
51
        } else {
52
            $this->default = $column['default'];
53
            $this->length = $column['length'];
54
            $this->type = $column['type'];
55
            $this->nulls = $column['nulls'];
56
        }        
57
    }
58
    
59
    public function type($type)
60
    {
61
        $this->type = $type;
62
        return $this;
63
    }
64
    
65
    public function nulls($nulls)
66
    {
67
       return $this->addChange('nulls', 'nulls', $nulls);
68
    }
69
    
70
    public function required(bool $required)
71
    {
72
        return $this->addChange('nulls', 'nulls', !$required);
73
    }
74
    
75
    public function defaultValue($default)
76
    {
77
        return $this->addChange('default', 'default', $default);
78
    }
79
80
    #[\Override]
81
    public function commitNew() 
82
    {
83
        $this->getDriver()->addColumn($this->buildDescription());        
0 ignored issues
show
Bug introduced by
The method addColumn() does not exist on yentu\ChangeLogger. 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

83
        $this->getDriver()->/** @scrutinizer ignore-call */ addColumn($this->buildDescription());        
Loading history...
84
    }
85
    
86
    public function length($length)
87
    {
88
        $this->length = $length;
89
        return $this;
90
    }
91
    
92
    public function drop()
93
    {
94
        $this->getDriver()->dropColumn($this->buildDescription());
0 ignored issues
show
Bug introduced by
The method dropColumn() does not exist on yentu\ChangeLogger. 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

94
        $this->getDriver()->/** @scrutinizer ignore-call */ dropColumn($this->buildDescription());
Loading history...
95
        return $this;
96
    }
97
}
98
99