Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original Code
- public function makeBooking($cargo, $voyage)
- {
- // This is an over-simplification of the logic for determining if a ship is over-booked
- $maxBooking = $voyage->getCapacity() * 1.1;
- if ($voyage->getBookedCargoSize() + $cargo->getSize() > $maxBooking) {
- return false;
- }
- return $voyage->addCargo($cargo);
- }
- // Alternative 1: Create a private method, or a method on the entity.
- public function makeBooking($cargo, $voyage)
- {
- $maxBooking = $this->getMaxBookingSize($voyage);
- if ($voyage->getBookedCargoSize() + $cargo->getSize() > $maxBooking) {
- return false;
- }
- return $voyage->addCargo($cargo);
- }
- private function getMaxBookingSize($voyage)
- {
- // This is a simplification of the logic for determining if a ship is over-booked
- if ($voyage->isDomestic()) {
- return $voyage->getCapacity() * 1.2;
- }
- return $voyage->getCapactiy() * 1.1;
- }
- // Alternative 2: Use a Policy class to keep the business logic explicit and easy to find / update
- class BookingPolicy
- {
- public function isAllowed($cargo, $voyage)
- {
- // This is a simplification of the logic for determining if a ship is over-booked
- if ($voyage->isDomestic()) {
- $maxBooking = $voyage->getCapacity() * 1.2;
- } else {
- $maxBooking = $voyage->getCapactiy() * 1.1;
- }
- return $voyage->getBookedCargoSize() + $cargo->getSize() > $maxBooking);
- }
- }
- public function makeBooking($cargo, $voyage)
- {
- if ($bookingPolicy->isAllowed($cargo, $voyage) {
- $voyage->addCargo($cargo);
- } else {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement