Insert::select()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\MySQL\Statement;
6
7
use QB\Generic\IQueryPart;
8
use QB\Generic\Statement\Insert as GenericInsert;
9
10
class Insert extends GenericInsert
11
{
12
    public const LOW_PRIORITY  = 'LOW_PRIORITY';
13
    public const HIGH_PRIORITY = 'HIGH_PRIORITY';
14
    public const DELAYED       = 'DELAYED';
15
    public const IGNORE        = 'IGNORE';
16
17
    /** @var array<int,string> */
18
    protected array $modifiers = [];
19
20
    protected ?IQueryPart $onDuplicateKeyUpdate = null;
21
22
    protected ?Select $select = null;
23
24
    /**
25
     * @param string ...$modifiers
26
     *
27
     * @return $this
28
     */
29 2
    public function modifier(string ...$modifiers): static
30
    {
31 2
        foreach ($modifiers as $modifier) {
32
            switch ($modifier) {
33 2
                case static::LOW_PRIORITY:
34 2
                case static::DELAYED:
35 2
                case static::HIGH_PRIORITY:
36 1
                    $this->modifiers[0] = $modifier;
37 1
                    break;
38 1
                case static::IGNORE:
39 1
                    $this->modifiers[1] = $modifier;
40 1
                    break;
41
            }
42
        }
43
44 2
        return $this;
45
    }
46
47
    /**
48
     * @param IQueryPart $onDuplicateKeyUpdate
49
     *
50
     * @return $this
51
     */
52 1
    public function setOnDuplicateKeyUpdate(IQueryPart $onDuplicateKeyUpdate): static
53
    {
54 1
        $this->onDuplicateKeyUpdate = $onDuplicateKeyUpdate;
55
56 1
        return $this;
57
    }
58
59
    /**
60
     * @param Select $select
61
     *
62
     * @return $this
63
     */
64 1
    public function select(Select $select): static
65
    {
66 1
        $this->select = $select;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 5
    public function __toString(): string
75
    {
76 5
        $parts = [parent::__toString()];
77
78 4
        if ($this->onDuplicateKeyUpdate) {
79 1
            $parts[] = sprintf('ON DUPLICATE KEY UPDATE %s', (string)$this->onDuplicateKeyUpdate);
80
        }
81
82 4
        return implode(PHP_EOL, $parts);
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 5
    public function isValid(): bool
89
    {
90 5
        return !empty($this->table) && (count($this->rawValues) > 0 || $this->select !== null);
91
    }
92
93
    /**
94
     * @return string[]
95
     */
96 4
    protected function getRawValues(): array
97
    {
98 4
        if ($this->select !== null) {
99 1
            return [(string)$this->select];
100
        }
101
102 3
        return parent::getRawValues();
103
    }
104
}
105