1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PH\Bundle\CoreBundle\Validator\Constraint; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Validator\Constraint; |
8
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
9
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
10
|
|
|
|
11
|
|
|
final class AmountRangeValidator extends ConstraintValidator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
* |
16
|
|
|
* @throws \Symfony\Component\Validator\Exception\UnexpectedTypeException |
17
|
|
|
*/ |
18
|
|
|
public function validate($value, Constraint $constraint) |
19
|
|
|
{ |
20
|
|
|
if (!$constraint instanceof AmountRange) { |
21
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AmountRange'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (null === $value) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (null === $value->getMethod()) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!is_numeric($value->getAmount())) { |
33
|
|
|
$this->context->buildViolation($constraint->invalidMessage) |
34
|
|
|
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE)) |
35
|
|
|
->setCode(AmountRange::INVALID_CHARACTERS_ERROR) |
36
|
|
|
->atPath('amount') |
37
|
|
|
->addViolation(); |
38
|
|
|
|
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$gatewayConfig = $value->getMethod()->getGatewayConfig()->getConfig(); |
43
|
|
|
|
44
|
|
|
if (!isset($gatewayConfig['minAmount']) && !isset($gatewayConfig['maxAmount'])) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$min = $gatewayConfig['minAmount']; |
49
|
|
|
$max = $gatewayConfig['maxAmount']; |
50
|
|
|
|
51
|
|
View Code Duplication |
if (null !== $max && null !== $value->getAmount() && $value->getAmount() > $max) { |
|
|
|
|
52
|
|
|
$this->context->buildViolation($constraint->maxMessage) |
53
|
|
|
->setParameter('{{ value }}', $this->formatValue($value->getAmount() / 100, self::PRETTY_DATE)) |
54
|
|
|
->setParameter('{{ limit }}', $this->formatValue($max / 100, self::PRETTY_DATE)) |
55
|
|
|
->setCode(AmountRange::TOO_HIGH_ERROR) |
56
|
|
|
->atPath('amount') |
57
|
|
|
->addViolation(); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
if (null !== $min && null !== $value->getAmount() && $value->getAmount() < $min) { |
|
|
|
|
63
|
|
|
$this->context->buildViolation($constraint->minMessage) |
64
|
|
|
->setParameter('{{ value }}', $this->formatValue($value->getAmount() / 100, self::PRETTY_DATE)) |
65
|
|
|
->setParameter('{{ limit }}', $this->formatValue($min / 100, self::PRETTY_DATE)) |
66
|
|
|
->setCode(AmountRange::TOO_LOW_ERROR) |
67
|
|
|
->atPath('amount') |
68
|
|
|
->addViolation(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.