TeamValueValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Validator\Constraints;
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 TeamValueValidator 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 TeamValue) {
25
            throw new UnexpectedTypeException($constraint, TeamValue::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