1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jean Silva <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Jeancsil\FlightSpy\History\ElasticSearch; |
7
|
|
|
|
8
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\Processor; |
9
|
|
|
|
10
|
|
|
class MappingProcessor implements Processor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $dataCache; |
16
|
|
|
|
17
|
|
|
/** @inheritdoc */ |
18
|
|
|
public function process(array $data) |
19
|
|
|
{ |
20
|
|
|
$this->dataCache = $data; |
21
|
|
|
|
22
|
|
|
$mappedDocuments = []; |
23
|
|
|
foreach ($data['Itineraries'] as $itinerary) { |
24
|
|
|
$outboundCarrier = $this->getCarrier($itinerary['OutboundLegId']); |
25
|
|
|
$inboundCarrier = $this->getCarrier($itinerary['InboundLegId']); |
|
|
|
|
26
|
|
|
|
27
|
|
|
foreach ($itinerary['PricingOptions'] as $priceOption) { |
28
|
|
|
$mappedData = []; |
|
|
|
|
29
|
|
|
$mappedData['Creation'] = $this->formatDate('now'); |
|
|
|
|
30
|
|
|
$mappedData['SessionKey'] = $data['SessionKey']; |
|
|
|
|
31
|
|
|
$mappedData['Status'] = $data['Status']; |
|
|
|
|
32
|
|
|
$mappedData['Adults'] = $data['Query']['Adults']; |
|
|
|
|
33
|
|
|
$mappedData['Children'] = $data['Query']['Children']; |
|
|
|
|
34
|
|
|
$mappedData['Infants'] = $data['Query']['Infants']; |
|
|
|
|
35
|
|
|
$mappedData['CabinClass'] = $data['Query']['CabinClass']; |
|
|
|
|
36
|
|
|
$mappedData['Country'] = $data['Query']['Country']; |
|
|
|
|
37
|
|
|
$mappedData['OriginPlace'] = $this->getPlaceNameById($data['Query']['OriginPlace']); |
|
|
|
|
38
|
|
|
$mappedData['DestinationPlace'] = $this->getPlaceNameById($data['Query']['DestinationPlace']); |
39
|
|
|
$mappedData['Locale'] = $data['Query']['Locale']; |
|
|
|
|
40
|
|
|
$mappedData['GroupPricing'] = $data['Query']['GroupPricing']; |
|
|
|
|
41
|
|
|
$mappedData['Departure'] = $this->getDepartureDate($itinerary['OutboundLegId']); |
|
|
|
|
42
|
|
|
$mappedData['Arrival'] = $this->getArrivalDate($itinerary['InboundLegId']); |
|
|
|
|
43
|
|
|
$mappedData['Price'] = $this->getPrice($priceOption); |
|
|
|
|
44
|
|
|
$mappedData['PriceFormatted'] = $this->getPrice($priceOption, $data['Query']['Currency']); |
|
|
|
|
45
|
|
|
$mappedData['OutboundAirline'] = $outboundCarrier['Name']; |
|
|
|
|
46
|
|
|
$mappedData['InboundAirline'] = $inboundCarrier['Name']; |
|
|
|
|
47
|
|
|
$mappedData['DeeplinkUrl'] = $priceOption['DeeplinkUrl']; |
|
|
|
|
48
|
|
|
|
49
|
|
|
$mappedDocuments[] = $mappedData; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->dataCache = null; |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $mappedDocuments; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function formatDate($date) |
59
|
|
|
{ |
60
|
|
|
return (new \DateTime($date)) |
61
|
|
|
->format(\DATE_ATOM); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getDepartureDate($legId) |
65
|
|
|
{ |
66
|
|
|
foreach ($this->dataCache['Legs'] as $leg) { |
67
|
|
|
if ($leg['Id'] == $legId) { |
68
|
|
|
return $this->formatDate($leg['Departure']); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getArrivalDate($legId) |
74
|
|
|
{ |
75
|
|
|
foreach ($this->dataCache['Legs'] as $leg) { |
76
|
|
|
if ($leg['Id'] == $legId) { |
77
|
|
|
return $this->formatDate($leg['Arrival']); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $placeId |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
private function getPlaceNameById($placeId) |
87
|
|
|
{ |
88
|
|
|
foreach ($this->dataCache['Places'] as $place) { |
89
|
|
|
if ($place['Id'] == $placeId) { |
90
|
|
|
return $place['Name']; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $legId |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function getCarrier($legId) |
100
|
|
|
{ |
101
|
|
|
foreach ($this->dataCache['Legs'] as $leg) { |
102
|
|
|
if ($leg['Id'] == $legId) { |
103
|
|
|
foreach ($this->dataCache['Carriers'] as $carrier) { |
104
|
|
|
if ($carrier['Id'] == $leg['Carriers'][0]) { |
105
|
|
|
return $carrier; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $priceOption |
114
|
|
|
* @param $currencyCode |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function getPrice($priceOption, $currencyCode = null) |
118
|
|
|
{ |
119
|
|
|
if ($currencyCode == null) { |
120
|
|
|
return $priceOption['Price']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
foreach ($this->dataCache['Currencies'] as $currency) { |
124
|
|
|
if ($currency['Code'] == $currencyCode) { |
125
|
|
|
if ($currency['SymbolOnLeft'] == 'true') { |
126
|
|
|
return $currency['Symbol'] . $priceOption['Price']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $priceOption['Price'] . $currency['Symbol']; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.