1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
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: 29.03.14 - 21:06 |
10
|
|
|
*/ |
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace Codeliner\CargoBackend\Application\Booking\Assembler; |
14
|
|
|
|
15
|
|
|
use Codeliner\CargoBackend\Application\Booking\Dto\LegDto; |
16
|
|
|
use Codeliner\CargoBackend\Application\Booking\Dto\RouteCandidateDto; |
17
|
|
|
use Codeliner\CargoBackend\Model\Cargo\Itinerary; |
18
|
|
|
use Codeliner\CargoBackend\Model\Cargo\Leg; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class RouteCandidateDtoAssembler |
22
|
|
|
* |
23
|
|
|
* @package CargoBackend\API\Booking\Assembler |
24
|
|
|
* @author Alexander Miertsch <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RouteCandidateDtoAssembler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param Itinerary $anItinerary |
30
|
|
|
* @return RouteCandidateDto |
31
|
|
|
*/ |
32
|
|
|
public function toDto(Itinerary $anItinerary): RouteCandidateDto |
33
|
|
|
{ |
34
|
|
|
$legs = array(); |
35
|
|
|
|
36
|
|
View Code Duplication |
foreach ($anItinerary->legs() as $leg) { |
|
|
|
|
37
|
|
|
$legDto = new LegDto(); |
38
|
|
|
|
39
|
|
|
$legDto->setLoadLocation($leg->loadLocation()); |
40
|
|
|
$legDto->setUnloadLocation($leg->unloadLocation()); |
41
|
|
|
$legDto->setLoadTime($leg->loadTime()->format(\DateTime::ATOM)); |
42
|
|
|
$legDto->setUnloadTime($leg->unloadTime()->format(\DateTime::ATOM)); |
43
|
|
|
|
44
|
|
|
$legs[] = $legDto; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$routeCandidate = new RouteCandidateDto(); |
48
|
|
|
|
49
|
|
|
$routeCandidate->setLegs($legs); |
50
|
|
|
|
51
|
|
|
return $routeCandidate; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param RouteCandidateDto $aRouteCandidate |
56
|
|
|
* @return Itinerary |
57
|
|
|
*/ |
58
|
|
|
public function toItinerary(RouteCandidateDto $aRouteCandidate): Itinerary |
59
|
|
|
{ |
60
|
|
|
$legs = array(); |
61
|
|
|
|
62
|
|
|
foreach ($aRouteCandidate->getLegs() as $legDto) { |
63
|
|
|
$legs[] = new Leg( |
64
|
|
|
$legDto->getLoadLocation(), |
65
|
|
|
$legDto->getUnloadLocation(), |
66
|
|
|
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getLoadTime()), |
67
|
|
|
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getUnloadTime()) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return new Itinerary($legs); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.