Limit   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 14
cts 20
cp 0.7
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 10 4
A toInteger() 0 4 1
A original() 0 4 1
A isMainQuery() 0 4 1
A isSupportQuery() 0 4 1
A inverse() 0 4 1
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Query;
4
5
use mav3rick177\RapidPagination\Base\Exceptions\Query\LimitParameterException;
6
7
/**
8
 * Class Limit
9
 */
10
class Limit
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $limit;
16
17
    /**
18
     * @var int
19
     */
20
    protected $originalLimit;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $isSupportQuery;
26
27
    /**
28
     * Limit constructor.
29
     *
30
     * @param int  $originalLimit
31
     * @param bool $isSupportQuery
32
     */
33 17
    public function __construct($originalLimit, $isSupportQuery = false)
34
    {
35 17
        $this->limit = static::validate($originalLimit, $isSupportQuery);
36 17
        $this->originalLimit = (int)$originalLimit;
37 17
        $this->isSupportQuery = (bool)$isSupportQuery;
38
    }
39
40
    /**
41
     * @param  int  $limit
42
     * @param  bool $isSupportQuery
43
     * @return int
44
     */
45 17
    protected static function validate($limit, $isSupportQuery)
46
    {
47 17
        if (!ctype_digit("$limit")) {
48
            throw new LimitParameterException('Limit must be integer');
49
        }
50 17
        if ($limit < 1) {
51
            throw new LimitParameterException('Limit must be positive integer');
52
        }
53 17
        return $isSupportQuery ? 1 : ($limit + 1);
54
    }
55
56
    /**
57
     * @return int
58
     */
59 17
    public function toInteger()
60
    {
61 17
        return $this->limit;
62
    }
63
64
    /**
65
     * @return int
66
     */
67 17
    public function original()
68
    {
69 17
        return $this->originalLimit;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function isMainQuery()
76
    {
77
        return !$this->isSupportQuery;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function isSupportQuery()
84
    {
85
        return $this->isSupportQuery;
86
    }
87
88
    /**
89
     * @return static
90
     */
91 10
    public function inverse()
92
    {
93 10
        return new static($this->originalLimit, !$this->isSupportQuery);
94
    }
95
}
96