Completed
Push — master ( 186a7f...bab9dc )
by Gabriel
10:46
created

Insert::parseOnDuplicate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 10.1554

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 3
cts 11
cp 0.2727
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 10.1554
1
<?php
2
3
4
namespace Nip\Database\Query;
5
6
/**
7
 * Class Insert
8
 * @package Nip\Database\Query
9
 */
10
class Insert extends AbstractQuery
11
{
12
    protected $_cols;
13
14
    protected $_values;
15
16
    /**
17
     * @return string
18
     */
19 1
    public function assemble()
20
    {
21 1
        $return = "INSERT INTO " . $this->protect($this->getTable());
22 1
        $return .= $this->parseCols();
23 1
        $return .= $this->parseValues();
24 1
        $return .= $this->parseOnDuplicate();
25
26 1
        return $return;
27
    }
28
29
    /**
30
     * @return string
31
     */
32 1
    public function parseCols()
33
    {
34 1
        if (is_array($this->parts['data'][0])) {
35 1
            $this->setCols(array_keys($this->parts['data'][0]));
36
        }
37 1
        return $this->_cols ? ' (' . implode(',', array_map([$this, 'protect'], $this->_cols)) . ')' : '';
38
    }
39
40
    /**
41
     * @param array|string $cols
42
     * @return $this
43
     */
44 1
    public function setCols($cols = null)
45
    {
46 1
        $this->_cols = $cols;
47 1
        return $this;
48
    }
49
50
    /**
51
     * @return string|false
52
     */
53 1
    public function parseValues()
54
    {
55 1
        if ($this->_values instanceof AbstractQuery) {
56
            return ' ' . (string) $this->_values;
57 1
        } elseif (is_array($this->parts['data'])) {
58 1
            return $this->parseData();
59
        }
60
        return false;
61
    }
62
63
    /**
64
     * Parses INSERT data
65
     *
66
     * @return string
67
     */
68 1
    protected function parseData()
69
    {
70 1
        $values = [];
71 1
        foreach ($this->parts['data'] as $key => $data) {
72 1
            foreach ($data as $value) {
73 1
                if (!is_array($value)) {
74 1
                    $value = [$value];
75
                }
76
77 1
                foreach ($value as $insertValue) {
78 1
                    $values[$key][] = $this->getManager()->getAdapter()->quote($insertValue);
79
                }
80
            }
81
        }
82 1
        foreach ($values as &$value) {
83 1
            $value = "(" . implode(", ", $value) . ")";
84
        }
85
86 1
        return ' VALUES ' . implode(', ', $values);
87
    }
88
89
    /**
90
     * @return string
91
     */
92 1
    public function parseOnDuplicate()
93
    {
94 1
        if ($this->hasPart('onDuplicate')) {
95
            $update = $this->getManager()->newUpdate();
96
97
            $onDuplicates = $this->getPart('onDuplicate');
98
            $data = [];
99
            foreach ($onDuplicates as $onDuplicate) {
100
                foreach ($onDuplicate as $key => $value) {
101
                    $data[$key] = $value;
102
                }
103
            }
104
            $update->data($data);
0 ignored issues
show
Unused Code introduced by
The call to Update::data() has too many arguments starting with $data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
106
            return " ON DUPLICATE KEY UPDATE {$update->parseUpdate()}";
107
        }
108 1
        return '';
109
    }
110
111
    public function setValues($values)
112
    {
113
        $this->_values = $values;
114
115
        return $this;
116
    }
117
118
    public function onDuplicate($value)
119
    {
120
        $this->addPart('onDuplicate', $value);
121
    }
122
}
123