Permalink
Switch branches/tags
Nothing to show
Find file Copy path
d9e6bfb Nov 19, 2015
Matt Allan cs fixes
0 contributors

Users who have contributed to this file

129 lines (108 sloc) 3.63 KB
<?php
namespace App\Http\Controllers;
use App\Foundation\MarshalsRequests;
use App\Http\Requests;
use App\Http\Transformers\DeliveryTransformer;
use Illuminate\Http\Exception\HttpResponseException;
use Illuminate\Http\Request;
use Quicksilver\Application\Delivery;
use Quicksilver\Domain\Delivery\Repository;
use Quicksilver\Domain\Delivery\Status;
class DeliveryController extends Controller
{
use MarshalsRequests;
/**
* @var Repository;
*/
private $deliveries;
/**
* DeliveryController constructor.
* @param Repository $deliveries
*/
public function __construct(Repository $deliveries)
{
$this->deliveries = $deliveries;
}
/**
* @return array
*/
public function index()
{
// For read methods we call the repository directly. You might want to create
// an application service for reading if you are adding query filters
// or any additional logic that needs to be reused outside of the controller.
$deliveries = $this->deliveries->findAll();
return fractal()
->collection($deliveries)
->transformWith(new DeliveryTransformer())
->toArray()
;
}
/**
* @param $deliveryId
* @return array
*/
public function show($deliveryId)
{
$delivery = $this->deliveries->find($deliveryId);
return fractal()
->item($delivery)
->transformWith(new DeliveryTransformer())
->toArray()
;
}
/**
* @param Delivery\Pickup $pickup
* @param Delivery\Deliver $deliver
* @param Request $httpRequest
* @param int $deliveryId
* @return array
*/
public function update(Delivery\Pickup $pickup, Delivery\Deliver $deliver, Request $httpRequest, $deliveryId)
{
// Rest APIs are pretty CRUDy, and there is only one update method for any update action.
// Since our domain is less CRUDy, we infer the intent of the action and
// call the appropriate application service.
if ($httpRequest->input('status') === Status::PICKED_UP) {
$request = $this->marshal(
Delivery\PickupRequest::class,
$httpRequest,
compact('deliveryId')
);
$delivery = $pickup($request);
} else if ($httpRequest->input('status') === Status::DELIVERED) {
$request = $this->marshal(
Delivery\DeliverRequest::class,
$httpRequest,
compact('deliveryId')
);
$delivery = $deliver($request);
} else {
throw new HttpResponseException(response('Unknown Request', 400));
}
return fractal()
->item($delivery)
->transformWith(new DeliveryTransformer())
->toArray()
;
}
/**
* @param Delivery\Create $create
* @param Request $httpRequest
* @return array
*/
public function store(Delivery\Create $create, Request $httpRequest)
{
// First we 'marshal' an application request from the HTTP request.
$request = $this->marshal(Delivery\CreateRequest::class, $httpRequest);
// Next we invoke our application service with the application request.
// Any failures throw exceptions, which we catch with middleware.
// If everything goes well, we get our response and render it.
$delivery = $create($request);
return fractal()
->item($delivery)
->transformWith(new DeliveryTransformer())
->toArray()
;
}
}