Passed
Push — main ( ddbd64...0c2f80 )
by Peter
02:39
created

Delete::addModifier()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 17
rs 9.5555
ccs 12
cts 12
cp 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\MySQL\Statement;
6
7
use QB\Generic\Statement\Delete as GenericDelete;
8
9
class Delete extends GenericDelete
10
{
11
    public const LOW_PRIORITY = 'LOW_PRIORITY';
12
    public const QUICK        = 'QUICK';
13
    public const IGNORE       = 'IGNORE';
14
15
    /** @var array<int,string> */
16
    protected array $modifiers = [];
17
18
    /** @var array<string,string> */
19
    protected array $orderByParts = [];
20
21
    protected ?int $limit = null;
22
23
    /**
24
     * @param string ...$modifiers
25
     *
26
     * @return $this
27
     */
28 1
    public function addModifier(string ...$modifiers): static
29
    {
30 1
        foreach ($modifiers as $modifier) {
31
            switch ($modifier) {
32 1
                case static::LOW_PRIORITY:
33 1
                    $this->modifiers[0] = $modifier;
34 1
                    break;
35 1
                case static::QUICK:
36 1
                    $this->modifiers[1] = $modifier;
37 1
                    break;
38 1
                case static::IGNORE:
39 1
                    $this->modifiers[2] = $modifier;
40 1
                    break;
41
            }
42
        }
43
44 1
        return $this;
45
    }
46
47
    /**
48
     * @param string $column
49
     * @param string $direction
50
     *
51
     * @return $this
52
     */
53 1
    public function addOrderBy(string $column, string $direction = 'ASC'): static
54
    {
55 1
        $this->orderByParts[$column] = $direction;
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * @param int|null $limit
62
     *
63
     * @return $this
64
     */
65 1
    public function setLimit(?int $limit): static
66
    {
67 1
        $this->limit = $limit;
68
69 1
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 4
    public function __toString(): string
76
    {
77 4
        $parts = array_merge(
78 4
            [parent::__toString()],
79 3
            $this->getOrderBy(),
80 3
            $this->getLimit(),
81
        );
82
83 3
        return implode(PHP_EOL, $parts);
84
    }
85
86 3
    protected function getOrderBy(): array
87
    {
88 3
        if (count($this->orderByParts) === 0) {
89 2
            return [];
90
        }
91
92 1
        $parts = [];
93 1
        foreach ($this->orderByParts as $column => $direction) {
94 1
            $parts[] = "$column $direction";
95
        }
96
97 1
        return ['ORDER BY ' . implode(', ', $parts)];
98
    }
99
100 3
    protected function getLimit(): array
101
    {
102 3
        $parts = [];
103 3
        if ($this->limit !== null) {
104 1
            $parts[] = sprintf('LIMIT %d', $this->limit);
105
        }
106
107 3
        return $parts;
108
    }
109
}
110