This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
/
DeliveryController.php
128 lines (108 loc) · 3.63 KB
/
DeliveryController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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()
;
}
}