InducementsAmountValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 12
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 20 5
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints;
4
5
use Obblm\Core\Helper\Rule\Inducement\Inducement;
6
use Symfony\Component\Form\Exception\UnexpectedTypeException;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\ConstraintValidator;
9
10
class InducementsAmountValidator extends ConstraintValidator
11
{
12
    public function validate($value, Constraint $constraint)
13
    {
14
        if (!$constraint instanceof InducementsAmount) {
15
            throw new UnexpectedTypeException($constraint, InducementsAmount::class);
16
        }
17
        if (!is_array($value)) {
18
            throw new UnexpectedTypeException($value, 'array');
19
        }
20
21
        $spend = 0;
22
23
        foreach ($value as $inducement) {
24
            /** @var Inducement $inducement */
25
            $spend += $inducement->getValue();
26
        }
27
28
        if ($spend > $constraint->budget) {
29
            $this->context->buildViolation($constraint->limitMessage)
30
                ->setParameter('{{ budget }}', $constraint->budgetToDisplay)
31
                ->addViolation();
32
        }
33
    }
34
}
35