NumberRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 0
loc 40
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 22 4
A supports() 0 9 3
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
5
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule;
6
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface;
7
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
8
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\Validator\Constraint;
11
use Symfony\Component\Validator\Constraints\Range;
12
use Symfony\Component\Validator\Constraints\Type;
13
14
/**
15
 * @author Warnar Boekkooi <[email protected]>
16
 */
17
class NumberRule implements ConstraintMapperInterface
18
{
19
    const RULE_NAME = 'number';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
25
    {
26 4
        if (!$this->supports($constraint, $form)) {
27 2
            throw new LogicException();
28
        }
29
30 2
        $message = null;
31 2
        if ($constraint instanceof Range) {
32 1
            $message = new RuleMessage($constraint->invalidMessage);
33 2
        } elseif ($constraint instanceof Type) {
34 1
            $message = new RuleMessage($constraint->message, array('{{ type }}' => $constraint->type));
35 1
        }
36 2
        $collection->set(
37 2
            self::RULE_NAME,
38 2
            new ConstraintRule(
39 2
                self::RULE_NAME,
40 2
                true,
41 2
                $message,
42 2
                $constraint->groups
43 2
            )
44 2
        );
45 2
    }
46
47 11
    public function supports(Constraint $constraint, FormInterface $form)
48
    {
49 11
        $class = get_class($constraint);
50
51 11
        return $class === Range::class || (
52 9
            $class === Type::class &&
53 5
            in_array(strtolower($constraint->type), array('int', 'integer', 'float', 'double'), true)
54 11
        );
55
    }
56
}
57