Completed
Push — master ( 5decc1...c80619 )
by Laurent
02:09
created

FlightValidator   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 127
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F isValid() 0 65 23
A isGroupedFlight() 0 4 2
A isHourValid() 0 5 2
A fetchService() 0 5 1
A getMinPrice() 0 6 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
     * @var Product
15
     */
16
    private $defaultService;
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function __construct(Translate $langs, DoliDB $db, $defaultServiceId)
22
    {
23
        parent::__construct($langs, $db);
24
        $this->fetchService($defaultServiceId);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function isValid($vol, $context = [])
31
    {
32
        if (!($vol instanceof Bbcvols)) {
33
            throw new \InvalidArgumentException('Flight validator only accepts a flight');
34
        }
35
36
        $this->valid = true;
37
38
        if (!$this->isHourValid($vol->heureD)) {
39
            $this->addError('heureD', "L'heure depart n'est pas correcte'");
40
        }
41
42
        if (!$this->isHourValid($vol->heureA)) {
43
            $this->addError('heureA', 'L\'heure d\'arrivee n\'est pas correcte');
44
        }
45
46
        if (empty($this->errors) && ($vol->heureA - $vol->heureD) <= 0) {
47
            $this->addError('heures', 'L\'heure de depart est plus grande  que l\'heure d\'arrivee');
48
        }
49
50
        if(empty($vol->lieuD)){
51
            $this->addError('lieuD', 'Le lieu de départ est vide');
52
        }
53
54
        if(empty($vol->lieuA)){
55
            $this->addError('lieuA', 'Le lieu d\'arrivée est vide');
56
        }
57
58
        // PAX
59
        if (!is_numeric($vol->nbrPax) || (int)$vol->nbrPax < 0) {
60
            $this->addError('nbrPax', 'Erreur le nombre de passager est un nombre négatif.');
61
        }
62
63
        if ($vol->mustHavePax() && !$vol->hasPax()) {
64
            $this->addError('nbrPax', 'Erreur ce type de vol doit etre fait avec des passagers.');
65
        }
66
67
        // verification billing
68
        if ($this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && $vol->isFree()) {
69
            $this->addError('cost', 'Erreur ce type de vol doit être payant.');
70
        }
71
72
        if ($vol->getFlightType()->isBillingRequired() && !$vol->hasReceiver()) {
73
            $this->addError('cost',
74
                'Erreur ce type de vol doit être payant, mais personne n\'a été signalé comme recepteur d\'argent.');
75
        }
76
77
        if($vol->getFlightType()->isBillingRequired() && ($vol->getAmountPerPassenger()) < $this->getMinPrice()){
78
            $this->addError('cost',
79
                sprintf('Le montant demandé pour ce vol n\'est pas suffisant un minimum de %s euros est demandé', $this->getMinPrice()));
80
        }
81
82
        //Kilometers
83
        if($vol->hasKilometers() && !$vol->getKilometers() > 0){
84
            $this->addError('kilometers',
85
                'Les kilometres doivent être un nombre positif');
86
        }
87
88
        if($vol->hasKilometers() && !$vol->hasKilometersDescription()){
89
            $this->addError('justif_kilometers',
90
                'Vous essayez d\'encoder des kilometres sans justificatif.');
91
        }
92
93
        return $this->valid;
94
    }
95
96
    /**
97
     * @param array $context
98
     *
99
     * @return bool
100
     */
101
    private function isGroupedFlight($context)
102
    {
103
        return key_exists('grouped_flight', $context) && !empty($context['grouped_flight']);
104
    }
105
106
    /**
107
     * @param string $hour
108
     *
109
     * @return bool
110
     */
111
    private function isHourValid($hour)
112
    {
113
        $patern = '#[0-9]{4}#';
114
        return !(preg_match($patern, $hour) == 0 || strlen($hour) != 4);
115
    }
116
117
    /**
118
     * @param $defaultServiceId
119
     */
120
    private function fetchService($defaultServiceId)
121
    {
122
        $this->defaultService = new Product($this->db);
123
        $this->defaultService->fetch($defaultServiceId);
124
    }
125
126
    /**
127
     * Returns the minimum price.
128
     *
129
     * @return int
130
     */
131
    public function getMinPrice(){
132
        if ($this->defaultService->price_base_type == 'TTC') {
133
            return $this->defaultService->price_min;
134
        }
135
        return $this->defaultService->price_min_ttc;
136
    }
137
}