Passed
Push — main ( c93099...0e69d1 )
by James Ekow Abaka
17:15
created

Table::initialize()   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
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace yentu\database;
3
4
5
class Table extends DatabaseItem implements Changeable, Initializable
6
{
7
    private Begin|Schema $schema;
8
    private bool $isReference;    
9
    public string $name;
10
    
11
    public function __construct(string $name,  Begin|Schema $schema)
12
    {
13
        $this->name = $name;
14
        $this->schema = $schema;
15
    }
16
    
17
    #[\Override]
18
    public function initialize(): void
19
    {
20
        if(!$this->getChangeLogger()->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

20
        if(!$this->getChangeLogger()->/** @scrutinizer ignore-call */ doesTableExist($this->buildDescription())) {
Loading history...
21
            $this->getChangeLogger()->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

21
            $this->getChangeLogger()->/** @scrutinizer ignore-call */ addTable($this->buildDescription());
Loading history...
22
            $this->new = true;
23
        }        
24
    }
25
    
26
    public function setIsReference($isReference): void
27
    {
28
        $this->isReference = $isReference;
29
    }
30
    
31
    public function isReference(): bool
32
    {
33
        return $this->isReference;
34
    }
35
    
36
    public function column($name): Column
37
    {
38
        return $this->factory->create(ItemType::Column, $name, $this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c...::Column, $name, $this) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\Column.
Loading history...
39
    }
40
    
41
    public function drop(): Table
42
    {
43
        $table = $this->getChangeLogger()->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

43
        $table = $this->getChangeLogger()->/** @scrutinizer ignore-call */ getDescription()->getTable(
Loading history...
44
            array(
45
                'table' => $this->name,
46
                'schema' => $this->schema->getName()
47
            )
48
        );
49
        $this->getChangeLogger()->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

49
        $this->getChangeLogger()->/** @scrutinizer ignore-call */ dropTable($table);
Loading history...
50
        return $this;
51
    }
52
    
53
    public function getName(): string
54
    {
55
        return $this->name;
56
    }
57
    
58
    public function getSchema(): Begin|Schema
59
    {
60
        return $this->schema;
61
    }
62
    
63
    public function primaryKey(string ...$args): PrimaryKey
64
    {
65
        return $this->factory->create(ItemType::PrimaryKey, $args, $this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c...imaryKey, $args, $this) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\PrimaryKey.
Loading history...
66
    }
67
    
68
    public function index(): Index
69
    {
70
        return $this->factory->create(ItemType::Index, func_get_args(), $this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c...func_get_args(), $this) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\Index.
Loading history...
71
    }
72
    
73
    public function unique(): UniqueKey
74
    {
75
        return $this->factory->create(ItemType::UniqueKey, func_get_args(), $this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c...func_get_args(), $this) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\UniqueKey.
Loading history...
76
    }
77
        
78
    public function foreignKey(string ... $args): ForeignKey
79
    {
80
        return $this->factory->create(ItemType::ForeignKey, $args, $this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c...reignKey, $args, $this) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\ForeignKey.
Loading history...
81
    }
82
    
83
    public function table($name): Table
84
    {
85
        return $this->factory->create(ItemType::Table, $name, $this->schema);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c..., $name, $this->schema) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\Table.
Loading history...
86
    }
87
    
88
    public function insert(array $columns, array $rows): Table
89
    {
90
        $this->getChangeLogger()->insertData(
0 ignored issues
show
Bug introduced by
The method insertData() 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

90
        $this->getChangeLogger()->/** @scrutinizer ignore-call */ insertData(
Loading history...
91
            ['columns'=>$columns, 'rows'=>$rows, 'schema'=>$this->schema->getName(), 'table' => $this->name]
92
        );
93
        return $this;
94
    }
95
    
96
    public function view($name): View
97
    {
98
        return $this->factory->create(ItemType::View, $name, $this->schema);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->c..., $name, $this->schema) returns the type yentu\database\DatabaseItem which includes types incompatible with the type-hinted return yentu\database\View.
Loading history...
99
    }
100
101
    #[\Override]
102
    public function buildDescription(): array
103
    {
104
        return array(
105
            'name' => $this->name,
106
            'schema' => $this->schema->getName()
107
        );
108
    }
109
}
110