Passed
Push — main ( eef78f...8587f9 )
by James Ekow Abaka
12:48
created

BasicKey::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
namespace yentu\database;
3
4
abstract class BasicKey extends DatabaseItem implements Commitable, Changeable //, Initializable
5
{
6
    protected array $columns = [];
7
    protected Table $table;
8
    protected ?string $name;
9
    
10
    public function __construct($columns, $table)
11
    {
12
        $this->columns = $columns;
13
        $this->table = $table;
14
    }
15
16
    #[\Override]
17
    public function isNew(): bool
18
    {
19
        if ($this->new === null) {
20
            $this->new = !$this->doesKeyExist($this->buildDescription());
21
        }
22
        return $this->new;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->new could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
23
    }
24
    
25
    abstract protected function doesKeyExist($constraint);
26
    abstract protected function addKey($constraint);
27
    abstract protected function dropKey($constraint);
28
    abstract protected function getNamePostfix();
29
    
30
    #[\Override]
31
    public function buildDescription()
32
    {
33
        return array(
34
            'table' => $this->table->getName(), 
35
            'schema' => $this->table->getSchema()->getName(), 
36
            'columns' => $this->columns,
37
            'name' => $this->name
38
                ?? $this->table->getName() . '_' . implode('_', $this->columns) . '_' . $this->getNamePostfix()
39
        );
40
    }
41
42
    #[\Override]
43
    public function commitNew() 
44
    {
45
        $this->addKey($this->buildDescription());
46
    }
47
    
48
    public function drop()
49
    {
50
        if (!$this->isNew()) {
51
            $this->dropKey($this->buildDescription());
52
        }
53
        return $this;
54
    }
55
    
56
    public function name($name)
57
    {
58
        $this->name = $name;
59
        $this->new = !$this->doesKeyExist($this->buildDescription());
60
        return $this;
61
    }
62
}
63
64