UpdateWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 48
c 1
b 0
f 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 18 2
A writeUpdateValues() 0 14 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 6/11/14
5
 * Time: 1:51 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
12
13
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
14
use NilPortugues\Sql\QueryBuilder\Manipulation\Update;
15
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
16
17
/**
18
 * Class UpdateWriter.
19
 */
20
class UpdateWriter extends AbstractBaseWriter
21
{
22
    /**
23
     * @param Update $update
24
     *
25
     * @throws QueryException
26
     *
27
     * @return string
28
     */
29
    public function write(Update $update)
30
    {
31
        $values = $update->getValues();
32
        if (empty($values)) {
33
            throw new QueryException('No values to update in Update query.');
34
        }
35
36
        $parts = array(
37
            'UPDATE '.$this->writer->writeTable($update->getTable()).' SET ',
38
            $this->writeUpdateValues($update),
39
        );
40
41
        AbstractBaseWriter::writeWhereCondition($update, $this->writer, $this->placeholderWriter, $parts);
42
        AbstractBaseWriter::writeLimitCondition($update, $this->placeholderWriter, $parts);
43
        $comment = AbstractBaseWriter::writeQueryComment($update);
44
45
        return $comment.implode(' ', $parts);
46
    }
47
48
    /**
49
     * @param Update $update
50
     *
51
     * @return string
52
     */
53
    private function writeUpdateValues(Update $update)
54
    {
55
        $assigns = [];
56
        foreach ($update->getValues() as $column => $value) {
57
            $newColumn = array($column);
58
            $column = $this->columnWriter->writeColumn(SyntaxFactory::createColumn($newColumn, $update->getTable()));
59
60
            $value = $this->writer->writePlaceholderValue($value);
61
62
            $assigns[] = "$column = $value";
63
        }
64
65
        return \implode(', ', $assigns);
66
    }
67
}
68