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

SimpleOrderValidator::isValid()   D

Complexity

Conditions 10
Paths 96

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 96
nop 2
dl 0
loc 30
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}