Passed
Push — main ( 440f49...00eeb9 )
by Peter
02:41
created

Delete::where()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Statement;
6
7
use QB\Generic\Clause\Table;
8
use QB\Generic\Expr\Expr;
9
use QB\Generic\IQueryPart;
10
11
class Delete implements IDelete
12
{
13
    /** @var array<int,string|Table> */
14
    protected array $tables = [];
15
16
    /** @var string[] */
17
    protected array $modifiers = [];
18
19
    /** @var IQueryPart[] */
20
    protected array $whereParts = [];
21
22
    /**
23
     * @param string|Table ...$tables
24
     *
25
     * @return $this
26
     */
27 12
    public function from(string|Table ...$tables): static
28
    {
29 12
        $this->tables = array_merge($this->tables, $tables);
30
31 12
        return $this;
32
    }
33
34
    /**
35
     * @param string|IQueryPart ...$whereParts
36
     *
37
     * @return $this
38
     */
39 8
    public function where(string|IQueryPart ...$whereParts): static
40
    {
41 8
        foreach ($whereParts as $wherePart) {
42 8
            $wherePart = is_string($wherePart) ? new Expr($wherePart) : $wherePart;
43
44 8
            $this->whereParts[] = $wherePart;
45
        }
46
47 8
        return $this;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 10
    public function __toString(): string
54
    {
55 10
        if (!$this->isValid()) {
56 2
            throw new \RuntimeException('Under-initialized DELETE query. Table and where are necessary');
57
        }
58
59 8
        $delete = $this->delete();
60
61 8
        $sqlParts = array_merge(
62 8
            [$delete],
63 8
            $this->getWhere(),
64
        );
65
66 8
        $sqlParts = array_filter($sqlParts);
67
68 8
        return implode(PHP_EOL, $sqlParts);
69
    }
70
71 10
    public function isValid(): bool
72
    {
73 10
        return count($this->tables) === 1 || count($this->whereParts) > 0;
74
    }
75
76 8
    protected function delete(): string
77
    {
78 8
        $sql = [];
79 8
        $sql[] = 'DELETE';
80 8
        $sql[] = $this->getModifiers();
81 8
        $sql[] = 'FROM';
82 8
        $sql[] = $this->tables[0];
83
84 8
        $sql = array_filter($sql);
85
86 8
        return implode(' ', $sql);
87
    }
88
89 8
    protected function getModifiers(): string
90
    {
91 8
        if (empty($this->modifiers)) {
92 7
            return '';
93
        }
94
95 1
        return implode(' ', $this->modifiers);
96
    }
97
98 8
    protected function getWhere(): array
99
    {
100 8
        if (count($this->whereParts) === 0) {
101 2
            return [];
102
        }
103
104 6
        $whereParts = [];
105 6
        foreach ($this->whereParts as $wherePart) {
106 6
            $whereParts[] = (string)$wherePart;
107
        }
108
109 6
        return ['WHERE ' . implode(' AND ', $whereParts)];
110
    }
111
112
    /**
113
     * @return array
114
     */
115 2
    public function getParams(): array
116
    {
117 2
        $params = [];
118
119 2
        foreach ($this->whereParts as $wherePart) {
120 2
            $params = array_merge($params, $wherePart->getParams());
121
        }
122
123 2
        return $params;
124
    }
125
}
126