Completed
Push — master ( c1ba47...3b776f )
by Benjamin
10:05 queued 04:46
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((string) $inducement, [], $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
        $starPlayers = $value->matching($criteria);
57
58
        if ($starPlayers->count() > $constraint->helper->getMaxStarPlayers()) {
59
            $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

59
            $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

59
            $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...
60
            $this->context->buildViolation($constraint->limitMessage)
61
                ->setParameter('{{ type }}', $type)
62
                ->setParameter('{{ limit }}', $constraint->helper->getMaxStarPlayers())
63
                ->addViolation();
64
        }
65
    }
66
}
67