Completed
Push — master ( 44aae5...b2077d )
by Nikola
01:16
created

OperationConstraint::fromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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