public function __construct($user, Trip $trip, $from, $to) { $this->user = $user; $this->trip = $trip; $this->date = $trip->getDate(); $this->from = $from; $this->to = $to; }
/** * @param User $user * @param $date * @param $lineNumber * @param $from * @param $to * @param $lineDeparture * @param int $continuation * @return Ticket|null */ public function buyTicket(User $user, $date, $lineNumber, $from, $to, $lineDeparture, $continuation = 0) { // Get Trip $trip = $this->documentManager->getRepository('AppBundle:Trip')->findOneBy(['date' => $date, 'lineNumber' => (int) $lineNumber, 'departure' => (int) $lineDeparture]); // If doesn't exists if (!$trip) { // Check lineNumber | from | to | lineDeparture if (!($line = $this->trainInformation->verifyLine($lineNumber, $from, $to, $lineDeparture))) { throw new BadRequestHttpException('Invalid trip.'); } // Create Trip $trip = new Trip($lineNumber, $line['stations'], $date, $lineDeparture, $this->trainInformation->getCapacity()); } // Get Train capacity if ($trip->getAvailableCapacity($from, $to) == 0) { throw new BadRequestHttpException('Trip is full.'); } if ($continuation == 1 && !$this->creditCardValidator->validate($user->getCreditCard())) { throw new BadRequestHttpException('Credit Card failed.'); } // Create Ticket $ticket = new Ticket($user, $trip, $from, $to); // Update Trip $trip->addTicketBought($from, $to); // Persist $this->documentManager->persist($ticket); $this->documentManager->persist($trip); $this->documentManager->flush(); return $ticket; }