Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

SkillsQuantityValidator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 44
c 1
b 0
f 0
dl 0
loc 65
ccs 0
cts 43
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 38 11
A initializeMaxValues() 0 8 5
A __construct() 0 3 1
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints\Team;
4
5
use Obblm\Core\Entity\TeamVersion;
6
use Obblm\Core\Helper\RuleHelper;
7
use Symfony\Component\Form\Exception\UnexpectedTypeException;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class SkillsQuantityValidator extends ConstraintValidator
12
{
13
    private $ruleHelper;
14
    private $helper = null;
15
    private $total = 0;
16
    private $max = ['total' => 0];
17
    private $count = ['total' => 0];
18
19
    public function __construct(RuleHelper $ruleHelper)
20
    {
21
        $this->ruleHelper = $ruleHelper;
22
    }
23
24
    public function validate($value, Constraint $constraint)
25
    {
26
        if (!$constraint instanceof SkillsQuantity) {
27
            throw new UnexpectedTypeException($constraint, SkillsQuantity::class);
28
        }
29
        if (!$value instanceof TeamVersion) {
30
            throw new UnexpectedTypeException($value, TeamVersion::class);
31
        }
32
        $this->max['total'] = $value->getTeam()->getCreationOption("skills_allowed.total") ?: 0;
33
        $this->initializeMaxValues($value, $constraint->context);
34
35
        $this->helper = $this->ruleHelper->getHelper($value->getTeam());
36
        foreach ($value->getNotDeadPlayerVersions() as $playerVersion) {
37
            if (count($playerVersion->getAdditionalSkills()) > $value->getTeam()->getCreationOption("skills_allowed.max_skills_per_player")) {
38
                $this->context->buildViolation($constraint->limitByPlayerMessage)
39
                    ->setParameter('{{ limit }}', $value->getTeam()->getCreationOption("skills_allowed.max_skills_per_player"))
0 ignored issues
show
Bug introduced by
It seems like $value->getTeam()->getCr...max_skills_per_player') can also be of type null; however, parameter $value of Symfony\Component\Valida...terface::setParameter() does only seem to accept string, 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

39
                    ->setParameter('{{ limit }}', /** @scrutinizer ignore-type */ $value->getTeam()->getCreationOption("skills_allowed.max_skills_per_player"))
Loading history...
40
                    ->addViolation();
41
                return;
42
            }
43
            foreach ($playerVersion->getAdditionalSkills() as $skillKey) {
44
                $skill = $this->helper->getSkill($skillKey);
45
                $context = $this->helper->getSkillContextForPlayerVersion($playerVersion, $skill);
46
                $this->count['total']++;
47
                if ($this->count['total'] > $this->max['total']) {
48
                    $this->context->buildViolation($constraint->limitMessage)
49
                        ->setParameter('{{ limit }}', $this->max['total'])
50
                        ->addViolation();
51
                    return;
52
                }
53
                if (isset($this->max[$context])) {
54
                    $this->count[$context]++;
55
                    $limit = $this->max[$context] ?: 0;
56
                    if ($this->count[$context] > $limit) {
57
                        $this->context->buildViolation($constraint->limitByTypeMessage)
58
                            ->setParameter('{{ type }}', $context)
59
                            ->setParameter('{{ limit }}', $limit)
60
                            ->addViolation();
61
                        return;
62
                    }
63
                }
64
            }
65
        }
66
    }
67
68
    private function initializeMaxValues(TeamVersion $value, array $context)
69
    {
70
        $this->total = 0;
71
        foreach ($context as $key) {
72
            if (is_string($key)) {
73
                $this->max[$key] = $value->getTeam()->getCreationOption("skills_allowed.$key") ?: 0;
74
                $this->count[$key] = 0;
75
                $this->total += $value->getTeam()->getCreationOption("skills_allowed.$key") ?: 0;
76
            }
77
        }
78
    }
79
}
80