Completed
Push — master ( 180ae9...7d1551 )
by Nikola
03:55
created

OperationConstraint   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 2
dl 0
loc 109
ccs 36
cts 48
cp 0.75
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A notEqualTo() 0 4 1
A greaterThan() 0 4 1
A greaterOrEqualTo() 0 4 1
A lessThan() 0 4 1
A lessOrEqualTo() 0 4 1
A fromString() 0 10 2
A getOperator() 0 4 1
A getOperand() 0 4 1
B assert() 0 17 7
A validateOperator() 0 12 3
A equalTo() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Comparison\Constraint;
6
7
use ReflectionClass;
8
use Version\Version;
9
use Version\Comparison\Exception\InvalidOperationConstraint;
10
11
class OperationConstraint implements Constraint
12
{
13
    public const OPERATOR_EQ = '=';
14
    public const OPERATOR_NEQ = '!=';
15
    public const OPERATOR_GT = '>';
16
    public const OPERATOR_GTE = '>=';
17
    public const OPERATOR_LT = '<';
18
    public const OPERATOR_LTE = '<=';
19
20
    /** @var string */
21
    protected $operator;
22
23
    /** @var Version */
24
    protected $operand;
25
26 16
    final public function __construct(string $operator, Version $operand)
27
    {
28 16
        $this->validateOperator($operator);
29
30 15
        $this->operator = $operator;
31 15
        $this->operand = $operand;
32 15
    }
33
34 1
    public static function equalTo(Version $operand)
35
    {
36 1
        return new static(self::OPERATOR_EQ, $operand);
37
    }
38
39
    public static function notEqualTo(Version $operand)
40
    {
41
        return new static(self::OPERATOR_NEQ, $operand);
42
    }
43
44
    public static function greaterThan(Version $operand)
45
    {
46
        return new static(self::OPERATOR_GT, $operand);
47
    }
48
49
    public static function greaterOrEqualTo(Version $operand)
50
    {
51
        return new static(self::OPERATOR_GTE, $operand);
52
    }
53
54
    public static function lessThan(Version $operand)
55
    {
56
        return new static(self::OPERATOR_LT, $operand);
57
    }
58
59
    public static function lessOrEqualTo(Version $operand)
60
    {
61
        return new static(self::OPERATOR_LTE, $operand);
62
    }
63
64
    /**
65
     * @param string $constraintString
66
     * @return OperationConstraint|CompositeConstraint
67
     */
68 10
    public static function fromString(string $constraintString)
69
    {
70 10
        static $parser = null;
71
72 10
        if (null === $parser) {
73 1
            $parser = new OperationConstraintParser();
74
        }
75
76 10
        return $parser->parse($constraintString);
77
    }
78
79 6
    public function getOperator(): string
80
    {
81 6
        return $this->operator;
82
    }
83
84 6
    public function getOperand(): Version
85
    {
86 6
        return $this->operand;
87
    }
88
89 10
    public function assert(Version $version): bool
90
    {
91 10
        switch ($this->operator) {
92 10
            case self::OPERATOR_EQ:
93 2
                return $version->isEqualTo($this->operand);
94 8
            case self::OPERATOR_NEQ:
95 1
                return !$version->isEqualTo($this->operand);
96 7
            case self::OPERATOR_GT:
97 2
                return $version->isGreaterThan($this->operand);
98 5
            case self::OPERATOR_GTE:
99 3
                return $version->isGreaterOrEqualTo($this->operand);
100 3
            case self::OPERATOR_LT:
101 2
                return $version->isLessThan($this->operand);
102 1
            case self::OPERATOR_LTE:
103 1
                return $version->isLessOrEqualTo($this->operand);
104
        }
105
    }
106
107 16
    protected function validateOperator(string $operator): void
108
    {
109 16
        static $validOperators = null;
110
111 16
        if (null === $validOperators) {
112
            $validOperators = (new ReflectionClass($this))->getConstants();
113
        }
114
115 16
        if (! in_array($operator, $validOperators, true)) {
116 1
            throw InvalidOperationConstraint::unsupportedOperator($operator);
117
        }
118 15
    }
119
}
120