コード例 #1
0
 public function testWrongCurrency()
 {
     $wrongFormats = array('lalla', '0000', '0', '', null, '----');
     foreach ($wrongFormats as $value) {
         $this->context->setPricePerNight($value);
         $result = $this->calculator->calculate($this->context);
         $this->assertTrue(isset($result['errors']), 'No price format failure ' . $value);
         $this->assertNotEmpty($result['errors']);
         $this->assertCount(1, $result['errors']);
     }
 }
コード例 #2
0
 /**
  * @Route("/add-booking/{pageId}")
  * @Method("POST")
  * @param Request $request
  * @throws \Exception
  * @return JsonResponse
  */
 public function addBookingAction($pageId, Request $request)
 {
     /**
      * @var Pages $page
      */
     $page = $this->getDoctrine()->getRepository('AppBundle:Pages')->find($pageId);
     if (!$page) {
         throw $this->createNotFoundException('No page found for request param ' . $pageId);
     }
     $response = new JsonResponse();
     $bookingEntity = new Booking();
     $bookingEntity->setPage($page);
     $form = $this->createForm(new BookingForm('/add-booking/' . $page->getId()), $bookingEntity);
     $form->handleRequest($request);
     $validatorBuilder = new BookingValidatorChainBuilder();
     $validatorChain = $validatorBuilder->buildChain();
     $values = $request->request->all();
     $bookingEntity->setPriceString($page->getPrice());
     if (isset($values['appbundle_booking'])) {
         $values = $values['appbundle_booking'];
     } else {
         $values = array();
     }
     $values['priceString'] = $page->getPrice();
     if ($validatorChain->validateContext($values) === null) {
         $calculator = new Calculator();
         $calcRequest = new CalculationContext();
         $calcRequest->setPricePerNight($values['priceString']);
         $calcRequest->setGuestsCount($values['guestsCount']);
         $calcRequest->setCheckOutDate($values['checkoutDate']);
         $calcRequest->setCheckInDate($values['checkinDate']);
         $calcResult = $calculator->calculate($calcRequest);
         $bookingEntity->setNightsCount($calcResult['nightsCount']);
         $bookingEntity->setTotal($calcResult['total']);
         $em = $this->getDoctrine()->getManager();
         $em->persist($bookingEntity);
         $em->flush();
         try {
             $this->sendBookingEmail($bookingEntity);
         } catch (\Exception $e) {
         }
         $response->setData(array('success' => true));
     } else {
         $response->setData(array('errors' => $validatorChain->getFlatenErrors()));
     }
     return $response;
 }
コード例 #3
0
ファイル: Calculator.php プロジェクト: breathbath/airbnbclone
 /**
  * @param CalculationContext $context
  * @return array
  */
 public function calculate(CalculationContext $context)
 {
     $errors = array();
     if (!($checkInDate = $this->createDateTime($context->getCheckInDate()))) {
         $errors[] = 'Wrong format of check in date';
     }
     if (!($checkOutDate = $this->createDateTime($context->getCheckOutDate()))) {
         $errors[] = 'Wrong format of check out date';
     }
     $price = new Currency();
     $priceValidator = new PriceValidator($price);
     if (!$priceValidator->validate($context->getPricePerNight(), array())) {
         $errors[] = $priceValidator->getErrorText();
     }
     $price->fromString($context->getPricePerNight());
     $number = (int) $price->getNumber();
     if (!empty($errors)) {
         return array('errors' => $errors);
     }
     if ($checkOutDate <= $checkInDate) {
         return array('errors' => array('Negative days count between ' . $context->getCheckInDate() . ' and ' . $context->getCheckOutDate()));
     }
     $nightsCount = (int) $checkOutDate->diff($checkInDate)->days;
     $price->setNumber($number * $nightsCount);
     return array('nightsCount' => $nightsCount, 'total' => $price->__toString(), 'pricePerNight' => $context->getPricePerNight(), 'checkinDate' => $context->getCheckInDate(), 'checkoutDate' => $context->getCheckOutDate(), "guestsCount" => $context->getGuestsCount());
 }