Update::buildUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Muffin\Queries;
4
5
use Muffin\Query;
6
use Muffin\Condition;
7
use Muffin\Traits\EscaperAware;
8
use Muffin\Snippet;
9
use Muffin\Queries\Snippets\Builders;
10
11
class Update implements Query
12
{
13
    use
14
        EscaperAware,
15
        Builders\Join,
16
        Builders\Where,
17
        Builders\OrderBy,
18
        Builders\Limit;
19
20
    private
21
        $updatePart,
22
        $sets;
23
24 6
    public function __construct($table = null, $alias = null)
25
    {
26 6
        $this->updatePart = new Snippets\Update();
27 6
        $this->where = new Snippets\Where();
28 6
        $this->sets = new Snippets\Set();
29 6
        $this->orderBy = new Snippets\OrderBy();
30
31 6
        if(! empty($table))
32 6
        {
33 3
            $this->update($table, $alias);
34 3
        }
35 6
    }
36
37 6 View Code Duplication
    public function toString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $queryParts = array(
40 6
            $this->buildUpdate(),
41 5
            $this->buildJoin(),
42 5
            $this->buildSets(),
43 5
            $this->buildWhere($this->escaper),
44 5
            $this->buildOrderBy(),
45 5
            $this->buildLimit(),
46 5
        );
47
48 5
        return implode(' ', array_filter($queryParts));
49
    }
50
51 5
    public function update($table, $alias = null)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
52
    {
53 5
        $this->updatePart->addTable($table, $alias);
54
55 5
        return $this;
56
    }
57
58 6
    public function set(array $fields)
59
    {
60 6
        $this->sets->set($fields);
61
62 6
        return $this;
63
    }
64
65 6
    private function buildUpdate()
66
    {
67 6
        $updateString = $this->updatePart->toString();
68
69 6
        if(empty($updateString))
70 6
        {
71 1
            throw new \RuntimeException('No table defined');
72
        }
73
74 5
        return $updateString;
75
    }
76
77 5
    private function buildSets()
78
    {
79 5
        $this->sets->setEscaper($this->escaper);
80
81 5
        return $this->sets->toString();
82
    }
83
}
84