Passed
Push — master ( bab9dc...bf7b05 )
by Gabriel
02:36
created

Update   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 49
ccs 13
cts 22
cp 0.5909
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assemble() 0 9 1
B parseUpdate() 0 29 9
1
<?php
2
3
namespace Nip\Database\Query;
4
5
/**
6
 * Class Update
7
 * @package Nip\Database\Query
8
 */
9
class Update extends AbstractQuery
10
{
11
12
    /**
13
     * @return string
14
     */
15
    public function assemble()
16
    {
17
        $query = "UPDATE {$this->protect($this->getTable())} SET {$this->parseUpdate()}";
18
19
        $query .= $this->assembleWhere();
20
        $query .= $this->assembleLimit();
21
22
        return $query;
23
    }
24
25
    /**
26
     * @return bool|string
27
     */
28 1
    public function parseUpdate()
29
    {
30 1
        if (!$this->parts['data']) {
31
            return false;
32
        }
33 1
        $fields = [];
34 1
        foreach ($this->parts['data'] as $data) {
35 1
            foreach ($data as $key => $values) {
36 1
                if (!is_array($values)) {
37
                    $values = [$values];
38
                }
39 1
                $value = $values[0];
40 1
                $quote = isset($values[1]) ? $values[1] : null;
41
42 1
                if (!is_numeric($value)) {
43 1
                    if (is_null($quote)) {
44
                        $quote = true;
45
                    }
46 1
                    if ($quote) {
47
                        $value = $this->getManager()->getAdapter()->quote($value);
48
                    }
49
                }
50
51 1
                $fields[] = "{$this->protect($key)} = $value";
52
            }
53
        }
54
55 1
        return implode(", ", $fields);
56
    }
57
}
58