1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/../exceptions/FlightNotFoundException.php'; |
4
|
|
|
require_once __DIR__ . '/../class/bbcvols.class.php'; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Laurent De Coninck <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class CreateFlightCommandHandler implements CommandHandlerInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var DoliDB |
13
|
|
|
*/ |
14
|
|
|
private $db; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var stdClass |
18
|
|
|
*/ |
19
|
|
|
private $conf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var User |
23
|
|
|
*/ |
24
|
|
|
private $user; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Translate |
28
|
|
|
*/ |
29
|
|
|
private $langs; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $validator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param DoliDB $db |
38
|
|
|
* @param stdClass $conf |
39
|
|
|
* @param User $user |
40
|
|
|
* @param Translate $langs |
41
|
|
|
* @param FlightValidator $validator |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
$db, |
45
|
|
|
$conf, |
46
|
|
|
$user, |
47
|
|
|
$langs, |
48
|
|
|
FlightValidator $validator |
49
|
|
|
) { |
50
|
|
|
$this->db = $db; |
51
|
|
|
$this->conf = $conf->global; |
52
|
|
|
$this->user = $user; |
53
|
|
|
$this->langs = $langs; |
54
|
|
|
$this->validator = $validator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param CreateFlightCommand|CommandInterface $command |
59
|
|
|
* |
60
|
|
|
* @return Bbcvols |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
|
|
public function handle(CommandInterface $command) |
64
|
|
|
{ |
65
|
|
|
$vol = new Bbcvols($this->db); |
66
|
|
|
$vol->date = $command->getDate(); |
|
|
|
|
67
|
|
|
$vol->lieuD = $command->getLieuD(); |
|
|
|
|
68
|
|
|
$vol->lieuA =$command->getLieuA(); |
|
|
|
|
69
|
|
|
$vol->heureD = $command->getHeureD(); |
|
|
|
|
70
|
|
|
$vol->heureA = $command->getHeureA(); |
|
|
|
|
71
|
|
|
$vol->BBC_ballons_idBBC_ballons = $command->getBBCBallonsIdBBCBallons(); |
|
|
|
|
72
|
|
|
$vol->nbrPax = $command->getNbrPax(); |
|
|
|
|
73
|
|
|
$vol->remarque = $command->getRemarque(); |
|
|
|
|
74
|
|
|
$vol->incidents = $command->getIncidents(); |
|
|
|
|
75
|
|
|
$vol->fk_type = $command->getFkType(); |
|
|
|
|
76
|
|
|
$vol->fk_pilot =$command->getFkPilot(); |
|
|
|
|
77
|
|
|
$vol->fk_organisateur = $command->getFkOrganisateur(); |
|
|
|
|
78
|
|
|
$vol->kilometers = $command->getKilometers(); |
|
|
|
|
79
|
|
|
$vol->cost = $command->getCost(); |
|
|
|
|
80
|
|
|
$vol->fk_receiver = $command->getFkReceiver(); |
|
|
|
|
81
|
|
|
$vol->justif_kilometers = $command->getJustifKilometers(); |
|
|
|
|
82
|
|
|
$vol->setPassengerNames($command->getPassengerNames()); |
|
|
|
|
83
|
|
|
$vol->setOrderId($command->getOrderId()); |
|
|
|
|
84
|
|
|
|
85
|
|
|
if (!$this->validator->isValid($vol, $_REQUEST)) { |
86
|
|
|
throw new Exception(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$result = $vol->create($this->user); |
90
|
|
|
if($result <= 0){ |
91
|
|
|
throw new Exception(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->handleOrder($vol); |
95
|
|
|
|
96
|
|
|
return $vol; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Handles the order of a flight. |
101
|
|
|
* |
102
|
|
|
* @param Bbcvols $flight |
103
|
|
|
* |
104
|
|
|
* @throws Exception |
105
|
|
|
*/ |
106
|
|
|
private function handleOrder($flight) |
107
|
|
|
{ |
108
|
|
|
$flight->fetch($flight->id); |
109
|
|
|
|
110
|
|
|
if(!$flight->isLinkedToOrder()){ |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$order = $this->getOrderFromFlight($flight); |
115
|
|
|
$order->add_object_linked('flightlog_bbcvols', $flight->getId()); |
116
|
|
|
|
117
|
|
|
$order->fetch_lines(); |
118
|
|
|
|
119
|
|
|
$qtyOrder = 0; |
120
|
|
|
/** @var OrderLine $currentOrderLine */ |
121
|
|
|
foreach($order->lines as $currentOrderLine){ |
122
|
|
|
$qtyOrder += (int)$currentOrderLine->qty; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$passangersCount = $this->numberOfPassengersLinkedToOrder($order->id); |
126
|
|
|
|
127
|
|
|
if($passangersCount != $qtyOrder){ |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if($order->statut == Commande::STATUS_VALIDATED){ |
132
|
|
|
$order->cloture($this->user); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Bbcvols $flight |
138
|
|
|
* |
139
|
|
|
* @return Commande |
140
|
|
|
* @throws Exception |
141
|
|
|
*/ |
142
|
|
|
private function getOrderFromFlight($flight) |
143
|
|
|
{ |
144
|
|
|
$flight->fetchOrder(); |
145
|
|
|
return $flight->getOrder(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param int $orderId |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
private function numberOfPassengersLinkedToOrder($orderId) |
154
|
|
|
{ |
155
|
|
|
$sql = sprintf('SELECT SUM(nbrPax) as total FROM `llx_bbc_vols` WHERE order_id = %s', $orderId); |
156
|
|
|
|
157
|
|
|
$resql = $this->db->query($sql); |
158
|
|
|
|
159
|
|
|
if ($resql) { |
160
|
|
|
$num = $this->db->num_rows($resql); |
161
|
|
|
$i = 0; |
162
|
|
|
if ($num) { |
163
|
|
|
while ($i < $num) { |
164
|
|
|
$result = $this->db->fetch_array($resql); |
165
|
|
|
return $result['total']; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return 0; |
171
|
|
|
} |
172
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: