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

AdditionalSkillsValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 22
c 1
b 0
f 0
dl 0
loc 45
ccs 0
cts 25
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addOtherValidator() 0 4 1
A __construct() 0 3 1
B validate() 0 30 9
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