1 | <?php |
||
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( |
||
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 | foreach($command->getOrderIds() as $orderId => $nbrPassengers){ |
||
84 | $vol->addOrderId($orderId, $nbrPassengers); |
||
85 | } |
||
86 | |||
87 | if (!$this->validator->isValid($vol, $_REQUEST)) { |
||
88 | throw new Exception(); |
||
89 | } |
||
90 | |||
91 | if(!$vol->getFlightType()->isBillingRequired() || $vol->isLinkedToOrder()){ |
||
92 | $vol->is_facture = true; |
||
93 | } |
||
94 | |||
95 | $result = $vol->create($this->user); |
||
96 | if($result <= 0){ |
||
97 | throw new Exception(); |
||
98 | } |
||
99 | |||
100 | $this->handleOrder($vol); |
||
101 | |||
102 | return $vol; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Handles the order of a flight. |
||
107 | * |
||
108 | * @param Bbcvols $flight |
||
109 | * |
||
110 | * @throws Exception |
||
111 | */ |
||
112 | private function handleOrder($flight) |
||
113 | { |
||
114 | $flight->fetch($flight->id); |
||
115 | |||
116 | if(!$flight->isLinkedToOrder()){ |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | foreach($flight->getOrders() as $currentOrder){ |
||
121 | $currentOrder->add_object_linked('flightlog_bbcvols', $flight->getId()); |
||
122 | $currentOrder->fetch_lines(); |
||
123 | |||
124 | $qtyOrder = 0; |
||
125 | /** @var OrderLine $currentOrderLine */ |
||
126 | foreach($currentOrder->lines as $currentOrderLine){ |
||
127 | $qtyOrder += (int)$currentOrderLine->qty; |
||
128 | } |
||
129 | |||
130 | $passangersCount = $this->numberOfPassengersLinkedToOrder($currentOrder->id); |
||
131 | |||
132 | if($passangersCount < $qtyOrder){ |
||
133 | continue; |
||
134 | } |
||
135 | |||
136 | if($currentOrder->statut == Commande::STATUS_VALIDATED){ |
||
137 | $currentOrder->cloture($this->user); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param int $orderId |
||
144 | * |
||
145 | * @return int |
||
146 | */ |
||
147 | private function numberOfPassengersLinkedToOrder($orderId) |
||
166 | } |
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: