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

SkillsQuantityValidator::validate()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 32
cp 0
rs 7.3166
cc 11
nc 9
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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