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

Table::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
namespace yentu\database;
3
4
use yentu\database\ItemType;
5
6
7
class Table extends DatabaseItem
8
{
9
    private Begin|Schema $schema;
10
    private array $primaryKeyColumns;
11
    private bool $isReference;    
12
    public string $name;
13
    
14
    public function __construct(string $name,  Begin|Schema $schema) 
15
    {
16
        $this->name = $name;
17
        $this->schema = $schema;
18
    }
19
    
20
    #[\Override]
21
    public function init()
22
    {
23
        if(!$this->getDriver()->doesTableExist($this->buildDescription())) {
0 ignored issues
show
Bug introduced by
The method doesTableExist() 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

23
        if(!$this->getDriver()->/** @scrutinizer ignore-call */ doesTableExist($this->buildDescription())) {
Loading history...
24
            $this->getDriver()->addTable($this->buildDescription());
0 ignored issues
show
Bug introduced by
The method addTable() 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

24
            $this->getDriver()->/** @scrutinizer ignore-call */ addTable($this->buildDescription());
Loading history...
25
            $this->new = true;
26
        }        
27
    }
28
    
29
    public function setIsReference($isReference)
30
    {
31
        $this->isReference = $isReference;
32
    }
33
    
34
    public function isReference()
35
    {
36
        return $this->isReference;
37
    }
38
    
39
    public function column($name)
40
    {
41
        return $this->factory->create(ItemType::Column, $name, $this);
42
    }
43
    
44
    public function drop()
45
    {
46
        $table = $this->getDriver()->getDescription()->getTable(
0 ignored issues
show
Bug introduced by
The method getDescription() 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

46
        $table = $this->getDriver()->/** @scrutinizer ignore-call */ getDescription()->getTable(
Loading history...
47
            array(
48
                'table' => $this->name,
49
                'schema' => $this->schema->getName()
50
            )
51
        );
52
        $this->getDriver()->dropTable($table);
0 ignored issues
show
Bug introduced by
The method dropTable() 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

52
        $this->getDriver()->/** @scrutinizer ignore-call */ dropTable($table);
Loading history...
53
        return $this;
54
    }
55
    
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
    
61
    public function getSchema()
62
    {
63
        return $this->schema;
64
    }
65
    
66
    public function primaryKey(string ...$args)
67
    {
68
        $this->primaryKeyColumns = $args;
69
        return $this->factory->create(ItemType::PrimaryKey, $args, $this);
70
    }
71
    
72
    public function index()
73
    {
74
        return $this->create('index', func_get_args(), $this);
0 ignored issues
show
Bug introduced by
The method create() does not exist on yentu\database\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

74
        return $this->/** @scrutinizer ignore-call */ create('index', func_get_args(), $this);
Loading history...
75
    }
76
    
77
    public function unique()
78
    {
79
        return $this->create('unique_key', func_get_args(), $this);
80
    }
81
        
82
    public function foreignKey(string ... $args)
83
    {
84
        return $this->factory->create(ItemType::ForeignKey, $args, $this);
85
    }
86
    
87
    public function table($name)
88
    {
89
        return $this->factory->create(ItemType::Table, $name, $this->schema);
90
    }  
91
    
92
    public function view($name)
93
    {
94
        return $this->create('view', $name, $this->schema);
95
    }
96
97
    #[\Override]
98
    protected function buildDescription() {
99
        return array(
100
            'name' => $this->name,
101
            'schema' => $this->schema->getName()
102
        );
103
    }
104
}
105