Completed
Pull Request — master (#9)
by Laurent
04:26 queued 02:17
created

SimpleOrderValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetchService() 0 5 1
D isValid() 0 30 10
A getMinPrice() 0 7 2
1
<?php
2
require_once __DIR__ . '/AbstractValidator.php';
3
require_once __DIR__ . '/../class/bbcvols.class.php';
4
5
6
/**
7
 * Validate a simple order.
8
 *
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
class SimpleOrderValidator 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
     * @param $defaultServiceId
29
     */
30
    private function fetchService($defaultServiceId)
31
    {
32
        $this->defaultService = new Product($this->db);
33
        $this->defaultService->fetch($defaultServiceId);
34
    }
35
36
    /**
37
     * Get a value as input and validates it. If an error occurs, it returns error messages.
38
     *
39
     * @param stdClass $value
40
     * @param array $context
41
     *
42
     * @return bool
43
     */
44
    public function isValid($value, $context = [])
45
    {
46
47
        $this->valid = true;
48
49
        if(empty($value->name)){
50
            $this->addError('name', 'Le nom est requis pour créer une commande');
51
        }
52
        if(empty($value->civilityId)){
53
            $this->addError('civility', 'Le titre de civilite est un champ requis.');
54
        }
55
        if(empty($value->email) && empty($value->phone)){
56
            $this->addError('email', 'Soit l\'e-mail soit le téléphone est requis');
57
        }
58
        if(empty($value->nbrPax) || (int)$value->nbrPax <= 0){
59
            $this->addError('nbrPax', 'Le nombre de passagers doit être plus grand que 0.');
60
        }
61
        if(empty($value->region)){
62
            $this->addError('region', 'Le lieu de décollage doit etre configuré');
63
        }
64
65
        if((int)$value->nbrPax > 0){
66
            $costPerPax = $value->cost / (int)$value->nbrPax;
67
            if($costPerPax < $this->getMinPrice()){
68
                $this->addError('cost', sprintf('Le prix demandé par passagé est trop peu élevé. Un minimum de %s est demandé', $this->getMinPrice()));
69
            }
70
        }
71
72
        return $this->valid;
73
    }
74
75
    /**
76
     * Returns the minimum price.
77
     *
78
     * @return int
79
     */
80
    private function getMinPrice()
81
    {
82
        if ($this->defaultService->price_base_type == 'TTC') {
83
            return $this->defaultService->price_min;
84
        }
85
        return $this->defaultService->price_min_ttc;
86
    }
87
}