IsSuffixValidValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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