Passed
Pull Request — development (#682)
by Nick
08:06
created

WaypointValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 2
A isOCWaypoint() 0 4 1
A isGCWaypoint() 0 4 1
1
<?php
2
3
namespace Oc\Validator\Constraints;
4
5
use Oc\GeoCache\Enum\WaypointType;
6
use Oc\GeoCache\Exception\UnknownWaypointTypeException;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\ConstraintValidator;
9
10
/**
11
 * Class WaypointValidator
12
 *
13
 * @package Oc\Validator\Constraints
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
14
 */
15
class WaypointValidator extends ConstraintValidator
16
{
17
    /**
18
     * Checks if the passed value is valid.
19
     *
20
     * @param mixed $waypoint The value that should be validated
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
21
     * @param Constraint $constraint The constraint for the validation
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
introduced by
Constraint => \Symfony\Component\Validator\Constraint
Loading history...
22
     *
23
     * @return bool
24
     */
25
    public function validate($waypoint, Constraint $constraint)
26
    {
27
        $valid = true;
28
29
        try {
30
            WaypointType::guess($waypoint);
31
        } catch (UnknownWaypointTypeException $e) {
32
            $this->context->buildViolation($constraint->messageInvalid)
33
                ->setParameter('%waypoint%', $waypoint)
34
                ->setInvalidValue($waypoint)
35
                ->addViolation();
36
            $valid = false;
37
        }
38
39
        return $valid;
40
    }
41
42
    /**
43
     * Check if the given waypoint is an oc waypoint.
44
     *
45
     * @param string $waypoint
46
     *
47
     * @return bool True if it is an oc waypoint
48
     */
49
    protected function isOCWaypoint($waypoint)
50
    {
51
        return stripos($waypoint, 'oc') === 0;
52
    }
53
54
    /**
55
     * Check if the given waypoint is an gc waypoint.
56
     *
57
     * @param string $waypoint
58
     *
59
     * @return bool True if it is an gc waypoint
60
     */
61
    protected function isGCWaypoint($waypoint)
62
    {
63
        return stripos($waypoint, 'gc') === 0;
64
    }
65
}
66