Where::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6667
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Muffin\Queries\Snippets;
4
5
use Muffin\Snippet;
6
use Muffin\Condition;
7
use Muffin\Traits\EscaperAware;
8
9
class Where implements Snippet
10
{
11
    use EscaperAware;
12
13
    private
14
        $conditions;
15
16 40
    public function __construct(Condition $condition = null)
17
    {
18 40
        $this->conditions = array();
19
20 40
        if($condition instanceof Condition)
21 40
        {
22 4
            $this->addCondition($condition);
23 4
        }
24 40
    }
25
26 24
    public function where(Condition $condition)
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...
27
    {
28 24
        $this->addCondition($condition);
29
30 24
        return $this;
31
    }
32
33 34
    public function toString()
34
    {
35 34
        $conditionString = $this->buildConditionString();
36 34
        if(empty($conditionString))
37 34
        {
38 8
            return '';
39
        }
40
41 27
        return sprintf('WHERE %s', $conditionString);
42
    }
43
44 34
    private function buildConditionString()
45
    {
46 34
        $whereConditions = array();
47 34
        foreach($this->conditions as $condition)
48
        {
49 28
            $conditionString = $condition->toString($this->escaper);
50 28
            if(! empty($conditionString))
51 28
            {
52 27
                $whereConditions[] = $conditionString;
53 27
            }
54 34
        }
55
56 34
        return implode(' AND ', $whereConditions);
57
    }
58
59 28
    private function addCondition(Condition $condition)
60
    {
61 28
        $this->conditions[] = $condition;
62 28
    }
63
}
64