Skip to content

Commit

Permalink
Add BookingSubscriber for new booking (Cocolabs-SAS#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
afedak authored and cocolabssas committed Jan 10, 2019
1 parent c6a3d87 commit c621bd7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
22 changes: 7 additions & 15 deletions src/Cocorico/CoreBundle/Controller/Frontend/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public function newAction(
\DateTime $end_time
) {
$dispatcher = $this->get('event_dispatcher');
$session = $this->container->get('session');
$translator = $this->container->get('translator');

$session = $this->get('session');
$translator = $this->get('translator');
$bookingHandler = $this->get('cocorico.form.handler.booking');

$booking = $bookingHandler->init(
$this->getUser(),
$listing,
Expand All @@ -83,10 +83,6 @@ public function newAction(
$end_time
);

$event = new BookingEvent($booking);
$dispatcher->dispatch(BookingEvents::BOOKING_INIT, $event);
$booking = $event->getBooking();

//Availability is validated through BookingValidator and amounts are setted through Form Event PRE_SET_DATA
$form = $this->createCreateForm($booking);

Expand All @@ -100,11 +96,7 @@ public function newAction(
$response = $event->getResponse();

if ($response === null) {//No response means we can create new booking
$booking = $this->get('cocorico.booking.manager')->create($event->getBooking());
if ($booking) {
$event->setBooking($booking);
$dispatcher->dispatch(BookingEvents::BOOKING_NEW_CREATED, $event);

//New Booking confirmation
$session->getFlashBag()->add(
'success',
Expand Down Expand Up @@ -157,8 +149,8 @@ public function newAction(
*/
private function addFormMessagesToFlashBag($success)
{
$session = $this->container->get('session');
$translator = $this->container->get('translator');
$session = $this->get('session');
$translator = $this->get('translator');

if ($success === 2) {//Voucher code is valid
$session->getFlashBag()->add(
Expand All @@ -185,7 +177,7 @@ private function addFormMessagesToFlashBag($success)
*/
private function addFlashError($success)
{
$translator = $this->container->get('translator');
$translator = $this->get('translator');
$errorMsg = $translator->trans('booking.new.unknown.error', array(), 'cocorico_booking'); //-4
$flashType = 'error';

Expand All @@ -207,7 +199,7 @@ private function addFlashError($success)
$flashType = 'error';
}

$this->container->get('session')->getFlashBag()->add(
$this->get('session')->getFlashBag()->add(
$flashType,
$errorMsg
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Cocorico\CoreBundle\Entity\Booking;
use Cocorico\CoreBundle\Entity\Listing;
use Cocorico\CoreBundle\Event\BookingEvent;
use Cocorico\CoreBundle\Event\BookingEvents;
use Cocorico\CoreBundle\Event\BookingFormEvent;
use Cocorico\CoreBundle\Event\BookingFormEvents;
use Cocorico\CoreBundle\Model\DateRange;
Expand Down Expand Up @@ -70,24 +72,42 @@ public function init(
\DateTime $startTime = null,
\DateTime $endTime = null
) {
$dateRange = $timeRange = null;
if ($start && $end) {
$dateRange = new DateRange($start, $end);
}
if ($startTime && $endTime) {
$timeRange = new TimeRange(
new \DateTime('1970-01-01 ' . $startTime->format('H:i')),
new \DateTime('1970-01-01 ' . $endTime->format('H:i'))
);
}
//Id of an eventual booking draft
$bookingId = $this->request->query->get('id');
//If no booking draft exists a new booking is initialized
if (!$bookingId) {
$dateRange = $timeRange = null;
if ($start && $end) {
$dateRange = new DateRange($start, $end);
}
if ($startTime && $endTime) {
$timeRange = new TimeRange(
new \DateTime('1970-01-01 ' . $startTime->format('H:i')),
new \DateTime('1970-01-01 ' . $endTime->format('H:i'))
);
}

//Get date range from post request if any
if ($this->request->getMethod() == 'POST') {
$dateRange = DateRange::createFromArray($this->request->request->get('date_range'));
$timeRange = TimeRange::createFromArray($this->request->request->get('time_range'));
}
//Get date range from post request if any
if ($this->request->getMethod() == 'POST') {
$dateRange = DateRange::createFromArray($this->request->request->get('date_range'));
$timeRange = TimeRange::createFromArray($this->request->request->get('time_range'));
}

$booking = $this->bookingManager->initBooking($listing, $user, $dateRange, $timeRange);

$booking = $this->bookingManager->initBooking($listing, $user, $dateRange, $timeRange);
$event = new BookingEvent($booking);
$this->dispatcher->dispatch(BookingEvents::BOOKING_INIT, $event);
$booking = $event->getBooking();
} else {
//If booking draft exists it is returned
$booking = $this->bookingManager->getRepository()->findOneBy(
array(
'id' => $bookingId,
'status' => Booking::STATUS_DRAFT,
'user' => $user->getId()
)
);
}

return $booking;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
services:
#Booking subscriber
cocorico.booking.subscriber:
class: Cocorico\CoreBundle\Event\BookingSubscriber
arguments:
- "@cocorico.booking.manager"
- "@event_dispatcher"
tags:
- { name: kernel.event_subscriber}

#Booking Refund subscriber
cocorico.booking_payin_refund.subscriber:
class: Cocorico\CoreBundle\Event\BookingPayinRefundSubscriber
Expand All @@ -22,4 +31,4 @@ services:
- "@cocorico.booking_bank_wire.manager"
- "@logger"
tags:
- { name: kernel.event_subscriber}
- { name: kernel.event_subscriber}

0 comments on commit c621bd7

Please sign in to comment.