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

CompositionValidator::validate()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 32
cp 0
rs 6.9666
cc 12
nc 49
nop 2
crap 156

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\Contracts\InducementInterface;
6
use Obblm\Core\Entity\Team;
7
use Obblm\Core\Entity\TeamVersion;
8
use Obblm\Core\Exception\UnexpectedTypeException;
9
use Obblm\Core\Helper\Rule\Inducement\StarPlayer;
10
use Obblm\Core\Helper\Rule\Translatable;
11
use Obblm\Core\Helper\RuleHelper;
12
use Obblm\Core\Helper\TeamHelper;
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
17
class CompositionValidator extends ConstraintValidator
18
{
19
    private $ruleHelper;
20
    private $translator;
21
22
    public function __construct(RuleHelper $ruleHelper, TranslatorInterface $translator)
23
    {
24
        $this->ruleHelper = $ruleHelper;
25
        $this->translator = $translator;
26
    }
27
28
    public function validate($value, Constraint $constraint)
29
    {
30
        if (!$constraint instanceof Composition) {
31
            throw new UnexpectedTypeException($constraint, Composition::class);
32
        }
33
34
        if ($value instanceof Team) {
35
            $value = TeamHelper::getLastVersion($value);
36
        }
37
38
        if (!$value instanceof TeamVersion) {
39
            throw new UnexpectedTypeException($value, TeamVersion::class);
40
        }
41
42
        $count = [];
43
        /** @var TeamVersion $value */
44
        $team  = $value->getTeam();
45
        $helper = $this->ruleHelper->getHelper($team);
46
        foreach ($value->getNotDeadPlayerVersions() as $version) {
47
            if ($version->getPlayer()->getPosition()) {
48
                $element = $helper->getPlayerPosition($version->getPlayer());
49
                $limit = $element->getMax();
50
                $position = $element->getKey();
51
                if (!$element instanceof Translatable) {
52
                    throw new UnexpectedTypeException($element, Translatable::class);
53
                }
54
                isset($count[$position]) ? $count[$position]++ : $count[$position] = 1;
55
                if ($count[$position] > $limit) {
56
                    $this->context->buildViolation($constraint->limitMessage)
57
                        ->setParameter('{{ limit }}', $limit)
58
                        ->setParameter('{{ player_type }}', $this->translator->trans($element->getName(), [], $element->getTranslationDomain()))
59
                        ->addViolation();
60
                }
61
                // Inducements
62
                if ($element instanceof StarPlayer) {
63
                    $position = $element->getTypeKey();
64
                    $limit = $helper->getMaxStarPlayers();
65
                    isset($count[$position]) ? $count[$position]++ : $count[$position] = 1;
66
                    if ($count[$position] > $limit) {
67
                        $this->context->buildViolation($constraint->limitMessage)
68
                            ->setParameter('{{ limit }}', $limit)
69
                            ->setParameter('{{ player_type }}', $this->translator->trans($element->getType(), [], $element->getType()->getTranslationDomain()))
70
                            ->addViolation();
71
                    }
72
                }
73
            }
74
        }
75
    }
76
}
77