Completed
Branch v1.x-dev (59bcf7)
by Benjamin
05:08
created

ValueValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 21 6
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 ValueValidator 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 Value) {
25
            throw new UnexpectedTypeException($constraint, Value::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
        $teamCost = $helper->calculateTeamValue($value);
36
        $limit = $helper->getMaxTeamCost();
37
38
        if ($teamCost > $limit) {
39
            $this->context->buildViolation($constraint->limitMessage)
40
                ->setParameter('{{ limit }}', $limit)
41
                ->setParameter('{{ current }}', $teamCost)
42
                ->addViolation();
43
        }
44
    }
45
}
46