Test Failed
Push — master ( 975525...307699 )
by Bjørn
02:39
created

IntegerOption   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkMinMax() 0 4 1
A check() 0 4 1
A checkMin() 0 6 3
A checkMax() 0 6 3
1
<?php
2
3
namespace WebPConvert\Options;
4
5
use WebPConvert\Options\Option;
6
use WebPConvert\Options\Exceptions\InvalidOptionValueException;
7
8
/**
9
 * Abstract option class
10
 *
11
 * @package    WebPConvert
12
 * @author     Bjørn Rosell <[email protected]>
13
 * @since      Class available since Release 2.0.0
14
 */
15
class IntegerOption extends Option
16
{
17
18
    protected $minValue;
19
    protected $maxValue;
20
21
    public function __construct($id, $defaultValue, $minValue = null, $maxValue = null)
22
    {
23
        $this->minValue = $minValue;
24
        $this->maxValue = $maxValue;
25
        parent::__construct($id, $defaultValue);
26
    }
27
28
    protected function checkMin()
29
    {
30
        if (!is_null($this->minValue) && $this->getValue() < $this->minValue) {
31
            throw new InvalidOptionValueException(
32
                '"' . $this->id . '" option must be set to minimum ' . $this->minValue . '. ' .
33
                'It was however set to: ' . $this->getValue()
34
            );
35
        }
36
    }
37
38
    protected function checkMax()
39
    {
40
        if (!is_null($this->maxValue) && $this->getValue() > $this->maxValue) {
41
            throw new InvalidOptionValueException(
42
                '"' . $this->id . '" option must be set to max ' . $this->maxValue . '. ' .
43
                'It was however set to: ' . $this->getValue()
44
            );
45
        }
46
    }
47
48
    protected function checkMinMax()
49
    {
50
        $this->checkMin();
51
        $this->checkMax();
52
    }
53
54
    public function check()
55
    {
56
        $this->checkType('integer');
57
        $this->checkMinMax();
58
    }
59
}
60