Completed
Push — master ( f1bd87...81d11b )
by Laurent
01:43
created

FlightValidator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
D isValid() 0 41 15
A isGroupedFlight() 0 4 2
A isHourValid() 0 5 2
1
<?php
2
3
require_once __DIR__ . '/AbstractValidator.php';
4
require_once __DIR__ . '/../class/bbcvols.class.php';
5
6
/**
7
 * Validates a flight
8
 *
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
class FlightValidator extends AbstractValidator
12
{
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function isValid($vol, $context = [])
18
    {
19
        if (!($vol instanceof Bbcvols)) {
20
            throw new \InvalidArgumentException('Flight validator only accepts a flight');
21
        }
22
23
        $this->valid = true;
24
25
        if (!$this->isHourValid($vol->heureD)) {
26
            $this->addError('heureD', "L'heure depart n'est pas correcte'");
27
        }
28
29
        if (!$this->isHourValid($vol->heureA)) {
30
            $this->addError('heureA', 'L\'heure d\'arrivee n\'est pas correcte');
31
        }
32
33
        if (empty($this->errors) && ($vol->heureA - $vol->heureD) <= 0) {
34
            $this->addError('heures', 'L\'heure de depart est plus grande  que l\'heure d\'arrivee');
35
        }
36
37
        // PAX
38
        if (!is_integer($vol->nbrPax) || $vol->nbrPax < 0) {
39
            $this->addError('nbrPax', 'Erreur le nombre de passager est un nombre négatif.');
40
        }
41
42
        if ($vol->mustHavePax() && !$vol->hasPax()) {
43
            $this->addError('nbrPax', 'Erreur ce type de vol doit etre fait avec des passagers.');
44
        }
45
46
        // verification billing
47
        if ($this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && $vol->isFree()) {
48
            $this->addError('cost', 'Erreur ce type de vol doit être payant.');
49
        }
50
51
        if ($vol->getFlightType()->isBillingRequired() && !$vol->hasReceiver()) {
52
            $this->addError('cost',
53
                'Erreur ce type de vol doit être payant, mais personne n\'a été signalé comme recepteur d\'argent.');
54
        }
55
56
        return $this->valid;
57
    }
58
59
    /**
60
     * @param array $context
61
     *
62
     * @return bool
63
     */
64
    private function isGroupedFlight($context)
65
    {
66
        return key_exists('grouped_flight', $context) && !empty($context['grouped_flight']);
67
    }
68
69
    /**
70
     * @param string $hour
71
     *
72
     * @return bool
73
     */
74
    private function isHourValid($hour)
75
    {
76
        $patern = '#[0-9]{4}#';
77
        return !(preg_match($patern, $hour) == 0 || strlen($hour) != 4);
78
    }
79
}