Passed
Push — master ( 8f2b8d...507611 )
by Andreas
17:50
created

urlnameValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 30 9
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\validation;
7
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
use midcom;
11
use midcom_helper_reflector_nameresolver;
12
use midcom_helper_misc;
13
use midcom\datamanager\storage\container\dbacontainer;
14
15
class urlnameValidator extends ConstraintValidator
16
{
17 2
    public function validate($value, Constraint $constraint)
18
    {
19 2
        if (empty($value)) {
20 1
            return;
21
        }
22
23 1
        $l10n = midcom::get()->i18n->get_l10n('midcom.datamanager');
24
25 1
        $data = $this->context->getRoot()->getData();
26 1
        if (!$data instanceof dbacontainer) {
27
            throw new \midcom_error('invalid storage, can only validate DBA objects');
28
        }
29
30 1
        $copy = clone $data->get_value();
31 1
        $property = $constraint->property['location'];
32 1
        $copy->{$property} = $value;
33 1
        $resolver = new midcom_helper_reflector_nameresolver($copy);
34
35 1
        $message = null;
36 1
        if (!$resolver->name_is_safe($property)) {
37
            $message = sprintf($l10n->get('type urlname: name is not "URL-safe", try "%s"'), midcom_helper_misc::urlize($value));
38 1
        } elseif (!$constraint->allow_unclean && !$resolver->name_is_clean($property)) {
39
            $message = sprintf($l10n->get('type urlname: name is not "clean", try "%s"'), midcom_helper_misc::urlize($value));
40 1
        } elseif (!$constraint->allow_catenate && !$resolver->name_is_unique()) {
41
            $message = sprintf($l10n->get('type urlname: name is already taken, try "%s"'), $resolver->generate_unique_name());
42
        }
43
44 1
        if (!empty($message)) {
45
            $this->context->buildViolation($message)
46
                ->addViolation();
47
        }
48 1
    }
49
}
50