UrlRule   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 37
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 24 3
A supports() 0 4 1
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\Url;
12
13
/**
14
 * @author Warnar Boekkooi <[email protected]>
15
 */
16
class UrlRule implements ConstraintMapperInterface
17
{
18
    const RULE_NAME = 'url';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
24
    {
25
        /** @var \Symfony\Component\Validator\Constraints\Url $constraint */
26 3
        if (!$this->supports($constraint, $form)) {
27 2
            throw new LogicException();
28
        }
29
30
        // jquery validate only validates https, http, ftp
31
        // So don't add the rule if there is some other protocol
32 1
        $diff = array_diff($constraint->protocols, array('http', 'https', 'ftp'));
33 1
        if (!empty($diff)) {
34
            return;
35
        }
36
37 1
        $collection->set(
38 1
            self::RULE_NAME,
39 1
            new ConstraintRule(
40 1
                self::RULE_NAME,
41 1
                true,
42 1
                new RuleMessage($constraint->message),
43 1
                $constraint->groups
44 1
            )
45 1
        );
46 1
    }
47
48 6
    public function supports(Constraint $constraint, FormInterface $form)
49
    {
50 6
        return get_class($constraint) === Url::class;
51
    }
52
}
53