Completed
Push — master ( 670622...a5ffbc )
by Edson
01:42
created

Statement   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 17 3
A create() 0 8 1
A select() 0 4 1
A all() 0 4 1
A delete() 0 4 1
1
<?php
2
3
namespace Keep;
4
5
trait Statement
6
{
7
    private $statement;
8
    private $attributes = [];
9
10 2
    public function create(string $table, array $attributes): self
11
    {
12 2
        $this->attributes = $attributes;
13 2
        $this->statement  = 'INSERT INTO `' . $table . '` (`';
14 2
        $this->statement .= Tool::getAttributesKey($attributes);
15 2
        $this->statement .= '`) VALUES (:';
16 2
        $this->statement .= Tool::getAttributesValue($attributes) . ')';
17 2
        return $this;
18
    }
19
20 2
    public function all(string $table): self
21
    {
22 2
        $this->statement = "SELECT * FROM `{$table}`";
23 2
        return $this;
24
    }
25
26 2
    public function select(string $table, array $columns): self
27
    {
28 2
        $this->statement = 'SELECT `' . Tool::columns($columns) . "` FROM `{$table}`";
29 2
        return $this;
30
    }
31
32 2
    public function update(string $table, array $attributes): self
33
    {
34 2
        $this->attributes = $attributes;
35
36 2
        $set = '';
37 2
        $key = array_keys($attributes);
38 2
        $pop = array_pop($key);
39
40 2
        foreach ($attributes as $key => $value) {
41 2
            $set .= ($key == $pop) ? '' : "`{$key}` = :{$key}, ";
42
        }
43
44 2
        $set .= "`{$pop}` = :{$pop}";
45
46 2
        $this->statement = "UPDATE `{$table}` SET {$set}";
47
48 2
        return $this;
49
    }
50
51 2
    public function delete(string $table): self
52
    {
53 2
        $this->statement = "DELETE FROM `{$table}`";
54 2
        return $this;
55
    }
56
}
57