|
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
|
|
|
$isGroupedFlight = $command->isGroupedFlight(); |
|
|
|
|
|
|
84
|
|
|
$vol->setOrderId($command->getOrderId()); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (!$this->validator->isValid($vol, $_REQUEST)) { |
|
87
|
|
|
throw new Exception(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$result = $vol->create($this->user); |
|
91
|
|
|
if($result <= 0){ |
|
92
|
|
|
throw new Exception(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if($command->hasOrderId()){ |
|
|
|
|
|
|
96
|
|
|
$order = new Commande($this->db); |
|
97
|
|
|
$order->fetch($command->getOrderId()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $vol; |
|
103
|
|
|
} |
|
104
|
|
|
} |
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: