FilterQuery::compile()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 8.7624
cc 5
eloc 11
nc 12
nop 1
crap 5
1
<?php
2
3
/**
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2015 Repo2
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
 * SOFTWARE.
25
 */
26
27
namespace Repo2\QueryBuilder\Query;
28
29
use Repo2\QueryBuilder\DriverInterface;
30
use Repo2\QueryBuilder\Expression\LogicCondition;
31
use Repo2\QueryBuilder\ExpressionBag;
32
use Repo2\QueryBuilder\ExpressionInterface;
33
34
abstract class FilterQuery implements ExpressionInterface
35
{
36
    /** @var LogicCondition */
37
    private $condition;
38
39
    /** @var int */
40
    private $limit;
41
42
    /** @var int */
43
    private $offset;
44
45
    /** @var ExpressionBag */
46
    private $orderBy;
47
48
    /**
49
     * @return $this
50
     */
51 12
    public function where()
52
    {
53 12
        $this->condition = new LogicCondition('AND', new ExpressionBag(func_get_args()));
54 12
        return $this;
55
    }
56
57
    /**
58
     * @param int $limit
59
     * @param int $offset
60
     * @return $this
61
     */
62 12
    public function limit($limit, $offset = 0)
63
    {
64 12
        $this->limit = $limit;
65 12
        $this->offset = $offset;
66 12
        return $this;
67
    }
68
69
    /**
70
     * @return $this
71
     */
72 9
    public function orderBy()
73
    {
74 9
        $this->orderBy = new ExpressionBag(func_get_args());
75 9
        return $this;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 45
    public function compile(DriverInterface $driver)
82
    {
83 45
        $compiled = $this->compileBase($driver);
84
85 39
        if ($this->condition) {
86 12
            $compiled .= ' WHERE ' . $this->condition->compile($driver);
87 8
        }
88
89 39
        if ($this->orderBy) {
90 9
            $compiled .= ' ORDER BY ' . $this->orderBy->concat($driver, ', ');
91 6
        }
92
93 39
        if ($this->limit) {
94 9
            $compiled .= ' LIMIT ' . (int) $this->limit;
95 9
            if ($this->offset) {
96 6
                $compiled .= ' OFFSET ' . (int) $this->offset;
97 4
            }
98 6
        }
99
100 39
        return $compiled;
101
    }
102
103
    /**
104
     * @param DriverInterface $driver
105
     * @return string
106
     */
107
    abstract protected function compileBase(DriverInterface $driver);
108
}
109