TeamCompositionValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints;
4
5
use Obblm\Core\Entity\Team;
6
use Obblm\Core\Entity\TeamVersion;
7
use Obblm\Core\Helper\PlayerHelper;
8
use Obblm\Core\Helper\TeamHelper;
9
use Symfony\Component\Form\Exception\UnexpectedTypeException;
10
use Symfony\Component\Validator\Constraint;
11
use Symfony\Component\Validator\ConstraintValidator;
12
13
class TeamCompositionValidator extends ConstraintValidator
14
{
15
    private $teamHelper;
16
17
    public function __construct(TeamHelper $teamHelper)
18
    {
19
        $this->teamHelper = $teamHelper;
20
    }
21
22
    public function validate($value, Constraint $constraint)
23
    {
24
        if (!$constraint instanceof TeamComposition) {
25
            throw new UnexpectedTypeException($constraint, TeamComposition::class);
26
        }
27
        if (!$value instanceof TeamVersion && !$value instanceof Team) {
28
            throw new UnexpectedTypeException($value, Team::class);
29
        }
30
        if ($value instanceof Team) {
31
            $value = TeamHelper::getLastVersion($value);
32
        }
33
        $count = [];
34
35
        /** @var TeamVersion $value */
36
37
        $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

37
        $maxPositions = $this->getMaxPlayersByTypes(/** @scrutinizer ignore-type */ $value->getTeam());
Loading history...
38
        foreach ($value->getNotDeadPlayerVersions() as $version) {
39
            if ($version->getPlayer()->getType()) {
40
                $limit = $maxPositions[$version->getPlayer()->getType()];
41
                $type = $version->getPlayer()->getType();
42
                isset($count[$type]) ? $count[$type]++ : $count[$type] = 1;
43
                if ($count[$type] > $limit) {
44
                    $this->context->buildViolation($constraint->limitMessage)
45
                        ->setParameter('{{ limit }}', $limit)
46
                        ->setParameter('{{ player_type }}', $type)
47
                        ->addViolation();
48
                }
49
            }
50
        }
51
    }
52
53
    protected function getMaxPlayersByTypes(Team $team):array
54
    {
55
        $helper = $this->teamHelper->getRuleHelper($team);
56
        $maxPositions = [];
57
58
        if ($helper->getAvailablePlayerTypes($team->getRoster())) {
59
            $types = $helper->getAvailablePlayerTypes($team->getRoster());
60
            foreach ($types as $key => $type) {
61
                $key = PlayerHelper::composePlayerKey($helper->getAttachedRule()->getRuleKey(), $team->getRoster(), $key);
62
                $maxPositions[$key] = $type['max'];
63
            }
64
        }
65
66
        return $maxPositions;
67
    }
68
}
69