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

BasicKey::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace yentu\database;
3
4
abstract class BasicKey extends DatabaseItem implements Commitable, Changeable, Initializable
5
{
6
    protected $columns;
7
    protected $table;
8
    protected $name;
9
    
10
    public function __construct($columns, $table)
11
    {
12
        $this->columns = $columns;
13
        $this->table = $table;
14
    }
15
    
16
    #[\Override]
17
    public function initialize(): void
18
    {
19
        $keyName = $this->doesKeyExist(array(
20
            'table' => $this->table->getName(),
21
            'schema' => $this->table->getSchema()->getName(),
22
            'columns' => $this->columns)
23
        );
24
        if($keyName === false)
25
        {
26
            $this->new = true;
27
            $this->name = $this->table->getName() . '_' . implode('_', $this->columns) . '_' . $this->getNamePostfix();
28
        }
29
        else
30
        {
31
            $this->name = $keyName;
32
        }        
33
    }
34
    
35
    abstract protected function doesKeyExist($constraint);
36
    abstract protected function addKey($constraint);
37
    abstract protected function dropKey($constraint);
38
    abstract protected function getNamePostfix();
39
    
40
    #[\Override]
41
    public function buildDescription()
42
    {
43
        return array(
44
            'table' => $this->table->getName(), 
45
            'schema' => $this->table->getSchema()->getName(), 
46
            'columns' => $this->columns,
47
            'name' => $this->name
48
        );
49
    }
50
51
    #[\Override]
52
    public function commitNew() 
53
    {
54
        $this->addKey($this->buildDescription());
55
    }
56
    
57
    public function drop()
58
    {
59
        $this->dropKey($this->getKeyDescription());
0 ignored issues
show
Bug introduced by
The method getKeyDescription() does not exist on yentu\database\BasicKey. 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

59
        $this->dropKey($this->/** @scrutinizer ignore-call */ getKeyDescription());
Loading history...
60
        return $this;
61
    }
62
    
63
    public function name($name)
64
    {
65
        $this->name = $name;
66
        return $this;
67
    }    
68
}
69
70