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

InducementsQuantityValidator::validate()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 26
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 41
rs 8.5706
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
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...
8
use Obblm\Core\Helper\Rule\Inducement\Inducement;
9
use Symfony\Component\Form\Exception\UnexpectedTypeException;
10
use Symfony\Component\Validator\Constraint;
11
use Symfony\Component\Validator\ConstraintValidator;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
class InducementsQuantityValidator extends ConstraintValidator
15
{
16
    private $translator;
17
18
    public function __construct(TranslatorInterface $translator)
19
    {
20
        $this->translator = $translator;
21
    }
22
23
    public function validate($value, Constraint $constraint)
24
    {
25
        if (!$constraint instanceof InducementsQuantity) {
26
            throw new UnexpectedTypeException($constraint, InducementsQuantity::class);
27
        }
28
        if (!is_array($value)) {
29
            throw new UnexpectedTypeException($value, Encounter::class);
30
        }
31
32
        $count = [];
33
34
        // Validate each inducement
35
        foreach ($value as $i => $inducement) {
36
            /** @var Inducement $inducement */
37
            if (!isset($count[$inducement->getKey()])) {
38
                $count[$inducement->getKey()] = 0;
39
            }
40
            $count[$inducement->getKey()]++;
41
            if ($count[$inducement->getKey()] > $inducement->getMax()) {
42
                $type = $this->translator->trans($inducement->getTranslationKey(), [], $inducement->getTranslationDomain());
43
                $this->context->buildViolation($constraint->limitMessage)
44
                    ->setParameter('{{ type }}', $type)
45
                    ->setParameter('{{ limit }}', $inducement->getMax())
46
                    ->addViolation();
47
            }
48
        }
49
50
        // Validate quantity of star players
51
        $value = new ArrayCollection($value);
52
        $criteria = (Criteria::create());
53
        $criteria->where(Criteria::expr()->eq('type', 'star_players'));
54
        $criteria->orderBy(['value' => 'ASC']);
55
56
        $star_players = $value->matching($criteria);
57
58
        if ($star_players->count() > $constraint->helper->getMaxStarPlayers()) {
59
            $type = $this->translator->trans($inducement->getTranslationType(), [], $inducement->getTranslationDomain());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inducement seems to be defined by a foreach iteration on line 35. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
60
            $this->context->buildViolation($constraint->limitMessage)
61
                ->setParameter('{{ type }}', $type)
62
                ->setParameter('{{ limit }}', $constraint->helper->getMaxStarPlayers())
63
                ->addViolation();
64
        }
65
    }
66
}
67