IsSuffixValidValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 18 5
1
<?php
2
3
namespace EmanueleMinotto\DomainParserBundle\Validator\Constraints;
4
5
use Pdp\Parser;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
/**
10
 * Constraint to check if the suffix is valid or not.
11
 *
12
 * @author Emanuele Minotto <[email protected]>
13
 */
14
class IsSuffixValidValidator extends ConstraintValidator
15
{
16
    /**
17
     * Pdp Parser.
18
     *
19
     * @var Parser
20
     */
21
    private $parser;
22
23
    /**
24
     * Contructor used for the parser dependency.
25
     *
26
     * @param Parser $parser
27
     */
28 12
    public function __construct(Parser $parser)
29
    {
30 12
        $this->parser = $parser;
31 12
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 12
    public function validate($value, Constraint $constraint)
37
    {
38 12
        // allow check for URLs too
39 6
        if ($host = parse_url($value, PHP_URL_HOST)) {
40
            $value = $host;
41
        }
42 6
43
        if (!$value || $this->parser->isSuffixValid($value)) {
44 6
            return;
45 6
        }
46 6
47 6
        $suffix = $this->parser->getPublicSuffix($value) ?: $value;
48 6
49
        $this->context
50
            ->buildViolation($constraint->message)
51
            ->setParameter('%suffix%', $suffix)
52
            ->addViolation();
53
    }
54
}
55