Passed
Push — master ( 08267b...db956f )
by Jan
04:49
created

BigDecimalGreaterThanValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 24
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compareValues() 0 11 4
A getErrorCode() 0 3 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Validator\Constraints\BigDecimal;
22
23
24
use Brick\Math\BigDecimal;
25
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
26
use Symfony\Component\Validator\Constraints\GreaterThan;
27
28
/**
29
 * Validates values are greater than the previous (>).
30
 *
31
 * @author Daniel Holmes <[email protected]>
32
 * @author Bernhard Schussek <[email protected]>
33
 */
34
class BigDecimalGreaterThanValidator extends AbstractComparisonValidator
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function compareValues($value1, $value2)
40
    {
41
        if ($value1 instanceof BigDecimal) {
42
            $value1 = (string) $value1;
43
        }
44
45
        if ($value2 instanceof BigDecimal) {
46
            $value2 = (string) $value2;
47
        }
48
49
        return null === $value2 || $value1 > $value2;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function getErrorCode()
56
    {
57
        return GreaterThan::TOO_LOW_ERROR;
58
    }
59
}