Completed
Branch develop (598d0f)
by Benjamin
03:23
created

InducementsAmountValidator::validate()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

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
rs 9.6111
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints;
4
5
use Obblm\Championship\Entity\Encounter;
0 ignored issues
show
Bug introduced by
The type Obblm\Championship\Entity\Encounter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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