Completed
Pull Request — master (#13)
by Nikola
01:52
created

Constraint::assert()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 14
cts 15
cp 0.9333
rs 8.2222
cc 7
eloc 16
nc 7
nop 1
crap 7.0145
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Constraint;
6
7
use Version\Version;
8
use Version\Exception\InvalidConstraintException;
9
use Version\Constraint\Parser\ParserInterface;
10
use Version\Constraint\Parser\StandardParser;
11
12
/**
13
 * @author Nikola Posa <[email protected]>
14
 */
15
class Constraint implements ConstraintInterface
16
{
17
    public const OPERATOR_EQ = '=';
18
    public const OPERATOR_NEQ = '!=';
19
    public const OPERATOR_GT = '>';
20
    public const OPERATOR_GTE = '>=';
21
    public const OPERATOR_LT = '<';
22
    public const OPERATOR_LTE = '<=';
23
24
    /**
25
     * @var string
26
     */
27
    protected $operator;
28
29
    /**
30
     * @var Version
31
     */
32
    protected $operand;
33
34
    /**
35
     * @var array
36
     */
37
    private static $validOperators = [
38
        self::OPERATOR_EQ,
39
        self::OPERATOR_NEQ,
40
        self::OPERATOR_GT,
41
        self::OPERATOR_GTE,
42
        self::OPERATOR_LT,
43
        self::OPERATOR_LTE,
44
    ];
45
46
    /**
47
     * @var ParserInterface
48
     */
49
    private static $parser;
50
51 15
    protected function __construct(string $operator, Version $operand)
52
    {
53 15
        $this->operator = $operator;
54 15
        $this->operand = $operand;
55 15
    }
56
57 16
    public static function fromProperties(string $operator, Version $operand) : Constraint
58
    {
59 16
        self::validateOperator($operator);
60
61 15
        return new self($operator, $operand);
62
    }
63
64 3
    public static function fromString(string $constraintString) : Constraint
65
    {
66 3
        return self::getParser()->parse($constraintString);
67
    }
68
69 16
    protected static function validateOperator(string $operator) : void
70
    {
71 16
        if (! in_array($operator, self::$validOperators, true)) {
72 1
            throw InvalidConstraintException::forOperator($operator);
73
        }
74 15
    }
75
76 3
    public static function getParser() : ParserInterface
77
    {
78 3
        if (null === self::$parser) {
79 1
            self::setParser(new StandardParser());
80
        }
81
82 3
        return self::$parser;
83
    }
84
85 1
    public static function setParser(ParserInterface $parser) : void
86
    {
87 1
        self::$parser = $parser;
88 1
    }
89
90 5
    public function getOperator() : string
91
    {
92 5
        return $this->operator;
93
    }
94
95 5
    public function getOperand() : Version
96
    {
97 5
        return $this->operand;
98
    }
99
100 11
    public function assert(Version $version) : bool
101
    {
102 11
        switch ($this->operator) {
103 11
            case self::OPERATOR_EQ:
104 2
                return $version->isEqualTo($this->operand);
105 9
            case self::OPERATOR_NEQ:
106 1
                return !$version->isEqualTo($this->operand);
107 8
            case self::OPERATOR_GT:
108 2
                return $version->isGreaterThan($this->operand);
109 6
            case self::OPERATOR_GTE:
110 3
                return $version->isGreaterOrEqualTo($this->operand);
111 4
            case self::OPERATOR_LT:
112 2
                return $version->isLessThan($this->operand);
113 2
            case self::OPERATOR_LTE:
114 2
                return $version->isLessOrEqualTo($this->operand);
115
            default:
116
                return false;
117
        }
118
    }
119
}
120