TeamCompositionValidator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 54
ccs 0
cts 32
cp 0
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B validate() 0 26 9
A getMaxPlayersByTypes() 0 14 3
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