|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the codeliner/php-ddd-cargo-sample package. |
| 4 | + * (c) Alexander Miertsch <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | +namespace Application\Controller; |
| 10 | + |
| 11 | +use Zend\Mvc\Controller\AbstractActionController; |
| 12 | +use Zend\View\Model\ViewModel; |
| 13 | +use Zend\Validator\StringLength; |
| 14 | +use Zend\Validator\Regex; |
| 15 | +use Application\Domain\Model\Cargo; |
| 16 | +use Application\Domain\Model\Voyage; |
| 17 | +use Application\Service\BookingService; |
| 18 | +use Application\Service\Exception\ServiceException; |
| 19 | +use Application\Service\Exception\RuntimeException; |
| 20 | +/** |
| 21 | + * MVC Controller that manages the booking process of Voyage. |
| 22 | + * |
| 23 | + * @author Alexander Miertsch <[email protected]> |
| 24 | + */ |
| 25 | +class BookingController extends AbstractActionController |
| 26 | +{ |
| 27 | + /** |
| 28 | + * The CargoRepository |
| 29 | + * |
| 30 | + * @var Cargo\CargoRepositoryInterface |
| 31 | + */ |
| 32 | + protected $cargoRepository; |
| 33 | + |
| 34 | + /** |
| 35 | + * |
| 36 | + * @var Voyage\VoyageRepositoryInterface |
| 37 | + */ |
| 38 | + protected $voyageRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * |
| 42 | + * @var BookingService |
| 43 | + */ |
| 44 | + protected $bookingService; |
| 45 | + |
| 46 | + /** |
| 47 | + * Book a Cargo on a Voyage |
| 48 | + */ |
| 49 | + public function bookingAction() |
| 50 | + { |
| 51 | + //we use the post redirect get pattern and redirect to same location |
| 52 | + $prg = $this->prg(); |
| 53 | + |
| 54 | + if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { |
| 55 | + // returned a response to redirect us |
| 56 | + return $prg; |
| 57 | + } elseif ($prg === false) { |
| 58 | + throw new RuntimeException('Request is out of date.'); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + $trackingId = $prg['tracking_id']; |
| 63 | + |
| 64 | + if (empty($trackingId)) { |
| 65 | + throw new RuntimeException('TrackingId must not be empty'); |
| 66 | + } |
| 67 | + |
| 68 | + $strLengthVal = new StringLength(1, 13); |
| 69 | + $regexVal = new Regex('/^[a-zA-Z0-9_-]+$/'); |
| 70 | + |
| 71 | + if (!$strLengthVal->isValid($trackingId) || !$regexVal->isValid($trackingId)) { |
| 72 | + throw new RuntimeException('TrackingId is invalid'); |
| 73 | + } |
| 74 | + |
| 75 | + $voyageNumber = $prg['voyage_number']; |
| 76 | + |
| 77 | + if (empty($voyageNumber)) { |
| 78 | + throw new RuntimeException('VoyageNumber must not be empty'); |
| 79 | + } |
| 80 | + |
| 81 | + $strLengthVal = new StringLength(3, 30); |
| 82 | + $regexVal = new Regex('/^[a-zA-Z0-9_-]+$/'); |
| 83 | + |
| 84 | + if (!$strLengthVal->isValid($voyageNumber) || !$regexVal->isValid($voyageNumber)) { |
| 85 | + throw new RuntimeException('VoyageNumber is invalid'); |
| 86 | + } |
| 87 | + |
| 88 | + $cargo = $this->cargoRepository->findCargo(new Cargo\TrackingId($trackingId)); |
| 89 | + |
| 90 | + if (is_null($cargo)) { |
| 91 | + throw new RuntimeException('Cargo can not be found'); |
| 92 | + } |
| 93 | + |
| 94 | + $voyage = $this->voyageRepository->findVoyage(new Voyage\VoyageNumber($voyageNumber)); |
| 95 | + |
| 96 | + if (is_null($voyage)) { |
| 97 | + throw new RuntimeException('Voyage can not be found'); |
| 98 | + } |
| 99 | + |
| 100 | + $this->bookingService->bookNewCargo($cargo, $voyage); |
| 101 | + |
| 102 | + return array('msg' => 'Cargo was successfully booked', 'success' => true); |
| 103 | + |
| 104 | + } catch (ServiceException $ex) { |
| 105 | + return array('msg' => $ex->getMessage(), 'success' => false); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Set the CargoRepository |
| 111 | + * |
| 112 | + * @param Cargo\CargoRepositoryInterface $cargoRepository |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function setCargoRepository(Cargo\CargoRepositoryInterface $cargoRepository) |
| 116 | + { |
| 117 | + $this->cargoRepository = $cargoRepository; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Set the voyage repository |
| 122 | + * |
| 123 | + * @param Voyage\VoyageRepositoryInterface $voyageRepository |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function setVoyageRepository(Voyage\VoyageRepositoryInterface $voyageRepository) |
| 127 | + { |
| 128 | + $this->voyageRepository = $voyageRepository; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Set BookingService |
| 133 | + * |
| 134 | + * @param BookingService $bookingService |
| 135 | + * @return void |
| 136 | + */ |
| 137 | + public function setBookingService(BookingService $bookingService) |
| 138 | + { |
| 139 | + $this->bookingService = $bookingService; |
| 140 | + } |
| 141 | +} |
0 commit comments