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

AdditionalSkillsValidator::validate()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 18
cp 0
rs 8.0555
cc 9
nc 18
nop 2
crap 90
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints\Team;
4
5
use Obblm\Core\Entity\Team;
6
use Obblm\Core\Entity\TeamVersion;
7
use Obblm\Core\Helper\RuleHelper;
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 AdditionalSkillsValidator extends ConstraintValidator
14
{
15
    protected $ruleHelper;
16
17
    public function __construct(RuleHelper $ruleHelper)
18
    {
19
        $this->ruleHelper = $ruleHelper;
20
    }
21
22
    public function validate($value, Constraint $constraint)
23
    {
24
        if (!$constraint instanceof AdditionalSkills) {
25
            throw new UnexpectedTypeException($constraint, AdditionalSkills::class);
26
        }
27
        if (!$value instanceof Team && !$value instanceof TeamVersion) {
28
            throw new UnexpectedTypeException($value, Team::class);
29
        }
30
        if ($value instanceof Team) {
31
            $value = TeamHelper::getLastVersion($value);
32
        }
33
34
        $helper = $this->ruleHelper->getHelper($value->getTeam());
35
36
        // Skills cost included in team value
37
        if ($value->getTeam()->getCreationOption('skills_allowed') &&
38
            $value->getTeam()->getCreationOption('skills_allowed.choice') == AdditionalSkills::NOT_FREE) {
39
            $helper->applyTeamExtraCosts($value, true);
40
            $this->addOtherValidator(new ValueValidator($this->ruleHelper), new Value(), $value);
41
        }
42
43
        // Skills quantity
44
        $skillContext = [];
45
        if ($value->getTeam()->getCreationOption('skills_allowed.single') !== null) {
46
            $skillContext[] = 'single';
47
        }
48
        if ($value->getTeam()->getCreationOption('skills_allowed.double') !== null) {
49
            $skillContext[] = 'double';
50
        }
51
        $this->addOtherValidator(new SkillsQuantityValidator($this->ruleHelper), new SkillsQuantity($skillContext), $value);
52
    }
53
54
    private function addOtherValidator(ConstraintValidator $validator, Constraint $constraint, $value)
55
    {
56
        $validator->initialize($this->context);
57
        $validator->validate($value, $constraint);
58
    }
59
}
60