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

PersistedWaypointValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 18 3
1
<?php
2
3
namespace Oc\Validator\Constraints;
4
5
use Oc\GeoCache\Persistence\GeoCache\GeoCacheService;
6
use Symfony\Component\Validator\Constraint;
7
8
/**
9
 * Class PersistedWaypointValidator
10
 *
11
 * @package Oc\Validator\Constraints
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
12
 */
13
class PersistedWaypointValidator extends WaypointValidator
14
{
15
    /**
16
     * @var GeoCacheService
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
17
     */
18
    private $geoCacheService;
19
20
    /**
21
     * PersistedWaypointValidator constructor.
22
     *
23
     * @param GeoCacheService $geoCacheService
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
24
     */
25
    public function __construct(GeoCacheService $geoCacheService)
26
    {
27
        $this->geoCacheService = $geoCacheService;
28
    }
29
30
    /**
31
     * Checks if the passed value is valid.
32
     *
33
     * @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...
34
     * @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...
35
     *
36
     * @return bool
37
     */
38
    public function validate($waypoint, Constraint $constraint)
39
    {
40
        $valid = parent::validate($waypoint, $constraint);
41
42
        if (!$valid) {
43
            return false;
44
        }
45
46
        $geoCache = $this->geoCacheService->fetchByWaypoint($waypoint);
47
48
        if ($geoCache === null) {
49
            $this->context->buildViolation($constraint->messageNotFound)
50
                ->setParameter('%waypoint%', $waypoint)
51
                ->addViolation();
52
        }
53
54
        return true;
55
    }
56
}
57