Passed
Push — main ( df4575...491aa8 )
by James Ekow Abaka
01:44
created

Table   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 112
rs 10
c 2
b 0
f 0
wmc 18

16 Methods

Rating   Name   Duplication   Size   Complexity  
A drop() 0 10 1
A getName() 0 3 1
A isReference() 0 3 1
A init() 0 6 2
A index() 0 3 1
A getSchema() 0 3 1
A column() 0 3 1
A unique() 0 3 1
A table() 0 3 1
A primaryKey() 0 4 1
A __construct() 0 4 1
A setIsReference() 0 3 1
A foreignKey() 0 3 1
A insert() 0 15 2
A view() 0 3 1
A buildDescription() 0 5 1
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 insert(array $columns, array $items)
93
    {
94
        $driver = $this->getDriver();
95
        $query = sprintf(
96
            "INSERT INTO %s (%s) VALUES (%s)",
97
            "{$driver->quoteIdentifier($this->schema->getName())}.{$driver->quoteIdentifier($this->name)}",
0 ignored issues
show
Bug introduced by
The method quoteIdentifier() 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

97
            "{$driver->/** @scrutinizer ignore-call */ quoteIdentifier($this->schema->getName())}.{$driver->quoteIdentifier($this->name)}",
Loading history...
98
            implode(", ", array_map(fn($x) => $driver->quoteIdentifier($x), $columns)),
99
            implode(", ", array_fill(0, count($items[0]), "?"))                             
100
        );
101
        
102
        foreach($items as $row) {
103
            $this->getDriver()->query($query, $row);
0 ignored issues
show
Bug introduced by
The method query() 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

103
            $this->getDriver()->/** @scrutinizer ignore-call */ query($query, $row);
Loading history...
104
        }
105
        
106
        return $this;
107
    }
108
    
109
    public function view($name)
110
    {
111
        return $this->create('view', $name, $this->schema);
112
    }
113
114
    #[\Override]
115
    protected function buildDescription() {
116
        return array(
117
            'name' => $this->name,
118
            'schema' => $this->schema->getName()
119
        );
120
    }
121
}
122