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

GroupOfPositionsValidator::validate()   B

Complexity

Conditions 9
Paths 27

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 23
cp 0
rs 8.0555
cc 9
nc 27
nop 2
crap 90
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 GroupOfPositionsValidator 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 GroupOfPositions) {
29
            throw new UnexpectedTypeException($constraint, GroupOfPositions::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
46
        $maxPositions = $this->getMaxPlayersByTypes($value->getTeam());
0 ignored issues
show
Bug introduced by
It seems like $value->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Validator\Con...:getMaxPlayersByTypes() 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

46
        $maxPositions = $this->getMaxPlayersByTypes(/** @scrutinizer ignore-type */ $value->getTeam());
Loading history...
47
        foreach ($value->getNotDeadPlayerVersions() as $version) {
48
            if ($version->getPlayer()->getPosition()) {
49
                $playerType = $version->getPlayer()->getPosition();
50
                list($ruleKey, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $playerType);
51
                $limit = $maxPositions[$type];
52
                isset($count[$type]) ? $count[$type]++ : $count[$type] = 1;
53
                if ($count[$type] > $limit) {
54
                    $translationName = CoreTranslation::getPlayerKeyType($ruleKey, $roster, $type);
55
                    $this->context->buildViolation($constraint->limitMessage)
56
                        ->setParameter('{{ limit }}', $limit)
57
                        ->setParameter('{{ player_type }}', $this->translator->trans($translationName, [], 'lrb6'))
58
                        ->addViolation();
59
                }
60
            }
61
        }
62
    }
63
64
    protected function getMaxPlayersByTypes(Team $team):array
65
    {
66
        $helper = $this->ruleHelper->getHelper($team);
67
        $maxPositions = [];
68
69
        if ($helper->getAvailablePlayerTypes($team->getRoster())) {
70
            $types = $helper->getAvailablePlayerTypes($team->getRoster());
71
            foreach ($types as $key => $type) {
72
                $maxPositions[$key] = $type['max'];
73
            }
74
        }
75
76
        return $maxPositions;
77
    }
78
}
79