Limit::limit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Muffin\Queries\Snippets\Builders;
4
5
use Muffin\Queries\Snippets;
6
7
trait Limit
8
{
9
    protected
10
        $limit,
11
        $offset;
12
13 7
    public function limit($limit)
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...
14
    {
15 7
        $this->limit = new Snippets\Limit($limit);
16
17 7
        return $this;
18
    }
19
20 6
    public function offset($offset)
21
    {
22 6
        if(!$this->limit instanceof Snippets\Limit)
23 6
        {
24 1
            throw new \LogicException('LIMIT is required to define OFFSET.');
25
        }
26
27 5
        $this->offset = new Snippets\Offset($offset);
28
29 5
        return $this;
30
    }
31
32 30
    private function buildLimit()
33
    {
34 30
        $limit = $this->buildLimitClause();
35
36 30
        $offset = '';
37 30
        if(! empty($limit))
38 30
        {
39 6
            $offset = $this->buildOffsetClause();
40 6
        }
41
42 30
        $clauses = array($limit, $offset);
43
44 30
        return implode(' ', array_filter($clauses));
45
    }
46
47 30
    private function buildLimitClause()
48
    {
49 30
        if($this->limit instanceof Snippets\Limit)
50 30
        {
51 7
            return $this->limit->toString();
52
        }
53 25
    }
54
55 6
    private function buildOffsetClause()
56
    {
57 6
        if($this->offset instanceof Snippets\Offset)
58 6
        {
59 4
            return $this->offset->toString();
60
        }
61 2
    }
62
}
63