Completed
Branch v1.x-dev (59bcf7)
by Benjamin
05:08
created

CompositionValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 26
c 1
b 0
f 0
dl 0
loc 42
ccs 0
cts 26
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 31 9
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints\Team;
4
5
use Obblm\Core\Entity\Team;
6
use Obblm\Core\Entity\TeamVersion;
7
use Obblm\Core\Helper\CoreTranslation;
8
use Obblm\Core\Helper\RuleHelper;
9
use Obblm\Core\Helper\TeamHelper;
10
use Symfony\Component\Form\Exception\UnexpectedTypeException;
11
use Symfony\Component\Validator\Constraint;
12
use Symfony\Component\Validator\ConstraintValidator;
13
use Symfony\Contracts\Translation\TranslatorInterface;
14
15
class CompositionValidator extends ConstraintValidator
16
{
17
    private $ruleHelper;
18
    private $translator;
19
20
    public function __construct(RuleHelper $ruleHelper, TranslatorInterface $translator)
21
    {
22
        $this->ruleHelper = $ruleHelper;
23
        $this->translator = $translator;
24
    }
25
26
    public function validate($value, Constraint $constraint)
27
    {
28
        if (!$constraint instanceof Composition) {
29
            throw new UnexpectedTypeException($constraint, Composition::class);
30
        }
31
32
        if ($value instanceof TeamVersion) {
33
            $value->getNotDeadPlayerVersions();
34
        }
35
36
        if (!$value instanceof TeamVersion) {
37
            throw new UnexpectedTypeException($value, TeamVersion::class);
38
        }
39
        if ($value instanceof Team) {
0 ignored issues
show
introduced by
$value is never a sub-type of Obblm\Core\Entity\Team.
Loading history...
40
            $value = TeamHelper::getLastVersion($value);
41
        }
42
        $count = [];
43
44
        /** @var TeamVersion $value */
45
        $team  = $value->getTeam();
46
        $helper = $this->ruleHelper->getHelper($team);
47
        foreach ($value->getNotDeadPlayerVersions() as $version) {
48
            if ($version->getPlayer()->getPosition()) {
49
                $position = $version->getPlayer()->getPosition();
50
                $basePosition = $helper->getRoster($team)->getPosition($position);
0 ignored issues
show
Bug introduced by
It seems like $team can also be of type null; however, parameter $team of Obblm\Core\Contracts\Rul...rInterface::getRoster() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

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

50
                $basePosition = $helper->getRoster(/** @scrutinizer ignore-type */ $team)->getPosition($position);
Loading history...
51
                isset($count[$position]) ? $count[$position]++ : $count[$position] = 1;
52
                if ($count[$position] > $basePosition->getMax()) {
53
                    $this->context->buildViolation($constraint->limitMessage)
54
                        ->setParameter('{{ limit }}', $basePosition->getMax())
55
                        ->setParameter('{{ player_type }}', $this->translator->trans($basePosition->getName(), [], $basePosition->getTranslationDomain()))
56
                        ->addViolation();
57
                }
58
            }
59
        }
60
    }
61
}
62