Completed
Push — master ( d99365...200168 )
by Jan
05:26
created

UrlOrBuiltinValidator::validate()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 13
nc 6
nop 2
dl 0
loc 25
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Validator\Constraints;
33
34
35
use Symfony\Component\Validator\Constraint;
36
use Symfony\Component\Validator\Constraints\UrlValidator;
37
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
38
use Symfony\Component\Validator\Exception\UnexpectedValueException;
39
40
/**
41
 * The validator for UrlOrBuiltin.
42
 * It checks if the value is either a builtin ressource or a valid url.
43
 * In both cases it is not checked, if the ressource is really existing.
44
 * @package App\Validator\Constraints
45
 */
46
class UrlOrBuiltinValidator extends UrlValidator
47
{
48
    public function validate($value, Constraint $constraint)
49
    {
50
        if (!$constraint instanceof UrlOrBuiltin) {
51
            throw new UnexpectedTypeException($constraint, UrlOrBuiltin::class);
52
        }
53
54
        if (null === $value || '' === $value) {
55
            return;
56
        }
57
        if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
58
            throw new UnexpectedValueException($value, 'string');
59
        }
60
        $value = (string) $value;
61
        if ('' === $value) {
62
            return;
63
        }
64
65
        //After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode
66
        $tmp = explode('/', $value);
67
        //Builtins must have a %PLACEHOLDER% construction
68
        if (!empty($tmp) && in_array($tmp[0], $constraint->allowed_placeholders, false)) {
69
            return;
70
        }
71
72
        parent::validate($value, $constraint); // TODO: Change the autogenerated stub
73
    }
74
75
76
}