Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

InducementsQuantityValidator::validate()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 41
ccs 0
cts 27
cp 0
rs 8.5706
cc 7
nc 12
nop 2
crap 56
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints\Team;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
use Obblm\Core\Helper\Rule\Inducement\Inducement;
8
use Symfony\Component\Form\Exception\UnexpectedTypeException;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Contracts\Translation\TranslatorInterface;
12
13
class InducementsQuantityValidator extends ConstraintValidator
14
{
15
    private $translator;
16
17
    public function __construct(TranslatorInterface $translator)
18
    {
19
        $this->translator = $translator;
20
    }
21
22
    public function validate($value, Constraint $constraint)
23
    {
24
        if (!$constraint instanceof InducementsQuantity) {
25
            throw new UnexpectedTypeException($constraint, InducementsQuantity::class);
26
        }
27
        if (!is_array($value)) {
28
            throw new UnexpectedTypeException($value, 'array');
29
        }
30
31
        $count = [];
32
33
        // Validate each inducement
34
        foreach ($value as $i => $inducement) {
35
            /** @var Inducement $inducement */
36
            if (!isset($count[$inducement->getKey()])) {
37
                $count[$inducement->getKey()] = 0;
38
            }
39
            $count[$inducement->getKey()]++;
40
            if ($count[$inducement->getKey()] > $inducement->getMax()) {
41
                $type = $this->translator->trans((string) $inducement, [], $inducement->getTranslationDomain());
42
                $this->context->buildViolation($constraint->limitMessage)
43
                    ->setParameter('{{ type }}', $type)
44
                    ->setParameter('{{ limit }}', $inducement->getMax())
45
                    ->addViolation();
46
            }
47
        }
48
49
        // Validate quantity of star players
50
        $value = new ArrayCollection($value);
51
        $criteria = (Criteria::create());
52
        $criteria->where(Criteria::expr()->eq('type', 'star_players'));
53
        $criteria->orderBy(['value' => 'ASC']);
54
55
        $starPlayers = $value->matching($criteria);
56
57
        if ($starPlayers->count() > $constraint->helper->getMaxStarPlayers()) {
58
            $type = $this->translator->trans($starPlayers->getTranslationType(), [], $starPlayers->getTranslationDomain());
0 ignored issues
show
Bug introduced by
The method getTranslationType() does not exist on Doctrine\Common\Collections\ArrayCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $type = $this->translator->trans($starPlayers->/** @scrutinizer ignore-call */ getTranslationType(), [], $starPlayers->getTranslationDomain());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getTranslationDomain() does not exist on Doctrine\Common\Collections\ArrayCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $type = $this->translator->trans($starPlayers->getTranslationType(), [], $starPlayers->/** @scrutinizer ignore-call */ getTranslationDomain());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
            $this->context->buildViolation($constraint->limitMessage)
60
                ->setParameter('{{ type }}', $type)
61
                ->setParameter('{{ limit }}', $constraint->helper->getMaxStarPlayers())
62
                ->addViolation();
63
        }
64
    }
65
}
66