Limit   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 76.6%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 96
ccs 36
cts 47
cp 0.766
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setLimit() 0 8 2
A getLimit() 0 4 1
A setOffset() 0 8 2
A getOffset() 0 4 1
A setPage() 0 5 1
A getPage() 0 4 1
A setPerPage() 0 7 2
A getPerPage() 0 4 1
A setPagingLimitOffset() 0 9 2
A buildEarly() 0 4 1
A build() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Sql\Component;
5
6
class Limit
7
{
8
    protected $limit = 0;
9
10
    protected $offset = 0;
11
12
    protected $page = 0;
13
14
    protected $perPage = 10;
15
16 2
    public function setLimit(int $limit): void
17
    {
18 2
        $this->limit = $limit;
19 2
        if ($this->page) {
20 1
            $this->page   = 0;
21 1
            $this->offset = 0;
22
        }
23 2
    }
24
25
    public function getLimit(): int
26
    {
27
        return $this->limit;
28
    }
29
30 2
    public function setOffset(int $offset): void
31
    {
32 2
        $this->offset = $offset;
33 2
        if ($this->page) {
34
            $this->page  = 0;
35
            $this->limit = 0;
36
        }
37 2
    }
38
39
    public function getOffset(): int
40
    {
41
        return $this->offset;
42
    }
43
44 2
    public function setPage(int $page): void
45
    {
46 2
        $this->page = $page;
47 2
        $this->setPagingLimitOffset();
48 2
    }
49
50
    public function getPage(): int
51
    {
52
        return $this->page;
53
    }
54
55 2
    public function setPerPage(int $perPage): void
56
    {
57 2
        $this->perPage = $perPage;
58 2
        if ($this->page) {
59
            $this->setPagingLimitOffset();
60
        }
61 2
    }
62
63
    public function getPerPage(): int
64
    {
65
        return $this->perPage;
66
    }
67
68 2
    protected function setPagingLimitOffset(): void
69
    {
70 2
        $this->limit  = 0;
71 2
        $this->offset = 0;
72 2
        if ($this->page) {
73 2
            $this->limit  = $this->perPage;
74 2
            $this->offset = $this->perPage * ($this->page - 1);
75
        }
76 2
    }
77
78 16
    public function buildEarly(): string
79
    {
80 16
        return '';
81
    }
82
83 16
    public function build(): string
84
    {
85 16
        $clause = '';
86
87 16
        if ($this->limit != 0) {
88 2
            $clause .= "LIMIT {$this->limit}";
89
        }
90
91 16
        if ($this->offset != 0) {
92 2
            $clause .= " OFFSET {$this->offset}";
93
        }
94
95 16
        if ($clause != '') {
96 2
            $clause = PHP_EOL . ltrim($clause);
97
        }
98
99 16
        return $clause;
100
    }
101
}
102