InducementsAmountValidator::validate()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 30
rs 9.6111
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