1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the prooph/cargo-sample2. |
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
|
|
|
* Date: 07.12.2015 - 6:30 PM |
10
|
|
|
*/ |
11
|
|
|
namespace Codeliner\CargoBackend\Http\Action; |
12
|
|
|
|
13
|
|
|
use Codeliner\CargoBackend\Application\Booking\BookingService; |
14
|
|
|
use Codeliner\CargoBackend\Application\Booking\Dto\LegDto; |
15
|
|
|
use Codeliner\CargoBackend\Application\Booking\Dto\RouteCandidateDto; |
16
|
|
|
use Codeliner\CargoBackend\Application\Exception\CargoNotFoundException; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
20
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class UpdateCargo |
24
|
|
|
* |
25
|
|
|
* @package Codeliner\CargoBackend\Application\Action |
26
|
|
|
*/ |
27
|
|
|
final class UpdateCargo |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var BookingService |
31
|
|
|
*/ |
32
|
|
|
private $bookingService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* UpdateCargo constructor. |
36
|
|
|
* |
37
|
|
|
* @param BookingService $bookingService |
38
|
|
|
*/ |
39
|
|
|
public function __construct(BookingService $bookingService) |
40
|
|
|
{ |
41
|
|
|
$this->bookingService = $bookingService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if (null === $trackingId = $request->getAttribute('trackingId')) { |
47
|
|
|
return new EmptyResponse(404); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$cargoData = $request->getParsedBody(); |
51
|
|
|
|
52
|
|
|
if (! isset($cargoData['legs']) || ! is_array($cargoData['legs'])) { |
53
|
|
|
throw new \InvalidArgumentException("Missing legs for cargo"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$routeCandidate = new RouteCandidateDto(); |
57
|
|
|
$routeCandidate->setLegs($this->toLegDtosFromData($cargoData['legs'])); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$this->bookingService->assignCargoToRoute($trackingId, $routeCandidate); |
61
|
|
|
|
62
|
|
|
$cargoRoutingDto = $this->bookingService->loadCargoForRouting($trackingId); |
63
|
|
|
|
64
|
|
|
return new JsonResponse($cargoRoutingDto->getArrayCopy()); |
65
|
|
|
}catch (CargoNotFoundException $e) { |
66
|
|
|
return new EmptyResponse(404); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $legs |
72
|
|
|
* @return LegDto[] |
73
|
|
|
*/ |
74
|
|
|
private function toLegDtosFromData(array $legs): array |
75
|
|
|
{ |
76
|
|
|
$legDtos = array(); |
77
|
|
|
|
78
|
|
|
foreach ($legs as $legData) { |
79
|
|
|
$legDto = new LegDto(); |
80
|
|
|
|
81
|
|
|
$legDto->setLoadLocation($legData['load_location']); |
82
|
|
|
$legDto->setUnloadLocation($legData['unload_location']); |
83
|
|
|
$legDto->setLoadTime($legData['load_time']); |
84
|
|
|
$legDto->setUnloadTime($legData['unload_time']); |
85
|
|
|
|
86
|
|
|
$legDtos[] = $legDto; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $legDtos; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.