Completed
Pull Request — master (#11)
by Romain
03:32
created

Select::buildFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 2.032
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
use Muffin\Queries\Snippets\Having;
11
use Muffin\QueryPartAware;
12
13
class Select implements Query, QueryPartAware
14
{
15
    use
16
        EscaperAware,
17
        Builders\Join,
18
        Builders\Where,
19
        Builders\GroupBy,
20
        Builders\OrderBy,
21
        Builders\Limit,
22
        Builders\QueryPart;
23
24
    private
25
        $select,
26
        $from,
27
        $having;
28
29 28
    public function __construct($columns = array())
30
    {
31 28
        $this->select = new Snippets\Select();
32 28
        $this->where = new Snippets\Where();
33 28
        $this->groupBy = new Snippets\GroupBy();
34 28
        $this->having = new Snippets\Having();
35 28
        $this->orderBy = new Snippets\OrderBy();
36
37 28
        $this->select->select($columns);
38 28
    }
39
40 27
    public function toString()
41
    {
42 27
        $snippets = $this->joins;
43 27
        $snippets[] = $this->from;
44 27
        $this->ensureNeededTablesArePresent($snippets);
45
46
        $queryParts = array(
47 24
            $this->buildSelect(),
48 23
            $this->buildFrom(),
49 23
            $this->buildJoin(),
50 23
            $this->buildWhere($this->escaper),
51 23
            $this->buildGroupBy(),
52 23
            $this->buildHaving(),
53 23
            $this->buildOrderBy(),
54 23
            $this->buildLimit(),
55 23
        );
56
57 23
        return implode(' ', array_filter($queryParts));
58
    }
59
60 26
    public function from($table, $alias = null)
61
    {
62 26
        $this->from = new Snippets\From($table, $alias);
63
64 26
        return $this;
65
    }
66
67 24
    public function select($columns)
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...
68
    {
69 24
        $this->select->select($columns);
70
71 24
        return $this;
72
    }
73
74 2
    public function having(Condition $condition)
75
    {
76 2
        $this->having->having($condition);
77
78 2
        return $this;
79
    }
80
81 24
    private function buildSelect()
82
    {
83 24
        return $this->select->toString();
84
    }
85
86 23
    private function buildFrom()
87
    {
88 23
        if(!$this->from instanceof Snippet)
89 23
        {
90
            throw new \LogicException('No column for FROM clause');
91
        }
92
93 23
        return $this->from->toString();
94
    }
95
96 23
    private function buildHaving()
97
    {
98 23
        $this->having->setEscaper($this->escaper);
99
100 23
        return $this->having->toString();
101
    }
102
}
103