Skip to content

Commit

Permalink
Add booking function
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed Nov 25, 2013
1 parent ca88ade commit 54e1dfa
Show file tree
Hide file tree
Showing 32 changed files with 831 additions and 60 deletions.
2 changes: 2 additions & 0 deletions data/DoctrineORMModule/Proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
15 changes: 14 additions & 1 deletion module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@
),
),
'service_manager' => array(
'invokables' => array(
'overbooking_policy' => 'Application\Service\Policy\TenPercentOverbookingPolicy',
),
'factories' => array(
'main_navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'cargo_form' => 'Application\Form\Service\CargoFormFactory',
'voyage_form' => 'Application\Form\Service\VoyageFormFactory',
'booking_service' => 'Application\Service\Factory\BookingServiceFactory',
'cargo_repository' => function($sl) {
$em = $sl->get('doctrine.entitymanager.orm_default');
return $em->getRepository('Application\Domain\Model\Cargo\Cargo');
Expand Down Expand Up @@ -120,6 +124,7 @@

$cargoController = new Application\Controller\CargoController();
$cargoController->setCargoRepository($cargoRepository);
$cargoController->setVoyageRepository($serviceManager->get('voyage_repository'));
$cargoController->setCargoForm($serviceManager->get('cargo_form'));
return $cargoController;
},
Expand All @@ -130,6 +135,15 @@
$voyageController->setVoyageForm($serviceManager->get('voyage_form'));
$voyageController->setVoyageRepository($serviceManager->get('voyage_repository'));
return $voyageController;
},
'Application\Controller\Booking' => function($controllerLoader) {
$serviceManager = $controllerLoader->getServiceLocator();

$bookingController = new Application\Controller\BookingController();
$bookingController->setCargoRepository($serviceManager->get('cargo_repository'));
$bookingController->setVoyageRepository($serviceManager->get('voyage_repository'));
$bookingController->setBookingService($serviceManager->get('booking_service'));
return $bookingController;
}
)
),
Expand All @@ -154,7 +168,6 @@
'orm_default' => array(
//Define custom doctrine types to map the ddd value objects
'types' => array(
'uid' => 'Application\Infrastructure\Persistence\Doctrine\Type\UID',
'trackingid' => 'Application\Infrastructure\Persistence\Doctrine\Type\TrackingId',
'voyagenumber' => 'Application\Infrastructure\Persistence\Doctrine\Type\VoyageNumber',
),
Expand Down
141 changes: 141 additions & 0 deletions module/Application/src/Application/Controller/BookingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/*
* This file is part of the codeliner/php-ddd-cargo-sample package.
* (c) Alexander Miertsch <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Validator\StringLength;
use Zend\Validator\Regex;
use Application\Domain\Model\Cargo;
use Application\Domain\Model\Voyage;
use Application\Service\BookingService;
use Application\Service\Exception\ServiceException;
use Application\Service\Exception\RuntimeException;
/**
* MVC Controller that manages the booking process of Voyage.
*
* @author Alexander Miertsch <[email protected]>
*/
class BookingController extends AbstractActionController
{
/**
* The CargoRepository
*
* @var Cargo\CargoRepositoryInterface
*/
protected $cargoRepository;

/**
*
* @var Voyage\VoyageRepositoryInterface
*/
protected $voyageRepository;

/**
*
* @var BookingService
*/
protected $bookingService;

/**
* Book a Cargo on a Voyage
*/
public function bookingAction()
{
//we use the post redirect get pattern and redirect to same location
$prg = $this->prg();

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
// returned a response to redirect us
return $prg;
} elseif ($prg === false) {
throw new RuntimeException('Request is out of date.');
}

try {
$trackingId = $prg['tracking_id'];

if (empty($trackingId)) {
throw new RuntimeException('TrackingId must not be empty');
}

$strLengthVal = new StringLength(1, 13);
$regexVal = new Regex('/^[a-zA-Z0-9_-]+$/');

if (!$strLengthVal->isValid($trackingId) || !$regexVal->isValid($trackingId)) {
throw new RuntimeException('TrackingId is invalid');
}

$voyageNumber = $prg['voyage_number'];

if (empty($voyageNumber)) {
throw new RuntimeException('VoyageNumber must not be empty');
}

$strLengthVal = new StringLength(3, 30);
$regexVal = new Regex('/^[a-zA-Z0-9_-]+$/');

if (!$strLengthVal->isValid($voyageNumber) || !$regexVal->isValid($voyageNumber)) {
throw new RuntimeException('VoyageNumber is invalid');
}

$cargo = $this->cargoRepository->findCargo(new Cargo\TrackingId($trackingId));

if (is_null($cargo)) {
throw new RuntimeException('Cargo can not be found');
}

$voyage = $this->voyageRepository->findVoyage(new Voyage\VoyageNumber($voyageNumber));

if (is_null($voyage)) {
throw new RuntimeException('Voyage can not be found');
}

$this->bookingService->bookNewCargo($cargo, $voyage);

return array('msg' => 'Cargo was successfully booked', 'success' => true);

} catch (ServiceException $ex) {
return array('msg' => $ex->getMessage(), 'success' => false);
}
}

/**
* Set the CargoRepository
*
* @param Cargo\CargoRepositoryInterface $cargoRepository
* @return void
*/
public function setCargoRepository(Cargo\CargoRepositoryInterface $cargoRepository)
{
$this->cargoRepository = $cargoRepository;
}

/**
* Set the voyage repository
*
* @param Voyage\VoyageRepositoryInterface $voyageRepository
* @return void
*/
public function setVoyageRepository(Voyage\VoyageRepositoryInterface $voyageRepository)
{
$this->voyageRepository = $voyageRepository;
}

/**
* Set BookingService
*
* @param BookingService $bookingService
* @return void
*/
public function setBookingService(BookingService $bookingService)
{
$this->bookingService = $bookingService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Domain\Model\Cargo;
use Application\Domain\Model\Voyage\VoyageRepositoryInterface;
use Application\Form\CargoForm;
/**
* MVC Controller for Cargo Management
Expand All @@ -26,6 +27,13 @@ class CargoController extends AbstractActionController
*/
protected $cargoRepository;

/**
*
* @var VoyageRepositoryInterface
*/
protected $voyageRepository;


/**
*
* @var CargoForm
Expand Down Expand Up @@ -56,7 +64,13 @@ public function showAction()
throw new \RuntimeException('Cargo can not be found. Please check the trackingId!');
}

return array('cargo' => $cargo);
if ($cargo->isBooked()) {
$voyages = array();
} else {
$voyages = $this->voyageRepository->findAll();
}

return array('cargo' => $cargo, 'voyages' => $voyages);
}

public function addAction()
Expand Down Expand Up @@ -103,6 +117,17 @@ public function setCargoRepository(Cargo\CargoRepositoryInterface $cargoReposito
$this->cargoRepository = $cargoRepository;
}

/**
* Set the voyage repository
*
* @param VoyageRepositoryInterface $voyageRepository
* @return void
*/
public function setVoyageRepository(VoyageRepositoryInterface $voyageRepository)
{
$this->voyageRepository = $voyageRepository;
}

/**
* Set a cargo form.
*
Expand Down
13 changes: 13 additions & 0 deletions module/Application/src/Application/Controller/VoyageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public function addAction()
}
}

public function showAction()
{
$voyageNumber = $this->getEvent()->getRouteMatch()->getParam('voyagenumber', '');

$voyage = $this->voyageRepository->findVoyage(new Voyage\VoyageNumber($voyageNumber));

if(is_null($voyage)) {
throw new \Exception('Voyage could not be found');
}

return array('voyage' => $voyage);
}

public function setVoyageRepository(Voyage\VoyageRepositoryInterface $voyageRepository)
{
$this->voyageRepository = $voyageRepository;
Expand Down
44 changes: 44 additions & 0 deletions module/Application/src/Application/Domain/Model/Cargo/Cargo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
namespace Application\Domain\Model\Cargo;

use Application\Domain\Shared\EntityInterface;
use Application\Domain\Model\Voyage\Voyage;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\JoinColumn;
/**
* A Cargo. This is the central class in the domain model.
*
Expand Down Expand Up @@ -49,6 +52,18 @@ class Cargo implements EntityInterface
* @var integer
*/
protected $size;

/**
* The booked Voyage
*
* --Annotations required by Doctrine----
* @ManyToOne(targetEntity="Application\Domain\Model\Voyage\Voyage", inversedBy="bookedCargos", fetch="LAZY")
* @JoinColumn(name="voyage_number", referencedColumnName="voyage_number")
* --------------------------------------
*
* @var Voyage
*/
protected $voyage;

/**
* Construct
Expand Down Expand Up @@ -90,7 +105,36 @@ public function setSize($size)
$this->size = $size;
}

/**
*
* @return Voyage
*/
public function getVoyage()
{
return $this->voyage;
}

/**
*
* @param Voyage $voyage
* @return void
*/
public function setVoyage(Voyage $voyage)
{
$this->voyage = $voyage;
}

/**
* Check if Cargo is already booked
*
* @return boolean
*/
public function isBooked()
{
return !is_null($this->getVoyage());
}


/**
* {@inheritDoc}
*/
Expand Down
Loading

0 comments on commit 54e1dfa

Please sign in to comment.