|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Jean Silva <[email protected]> |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Jeancsil\FlightSpy\Api\Processor; |
|
7
|
|
|
|
|
8
|
|
|
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters; |
|
9
|
|
|
use Jeancsil\FlightSpy\Notifier\Deal; |
|
10
|
|
|
use Jeancsil\FlightSpy\Notifier\NotifiableInterface; |
|
11
|
|
|
use Jeancsil\FlightSpy\Service\Currency\PriceFormatter; |
|
12
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
class LivePricePostProcessor |
|
15
|
|
|
{ |
|
16
|
|
|
use LoggerAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
const MAX_PARSED_DEALS = 50; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var PriceFormatter |
|
22
|
|
|
*/ |
|
23
|
|
|
private $priceFormatter; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var NotifiableInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $notifiers = []; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var SessionParameters |
|
31
|
|
|
*/ |
|
32
|
|
|
private $sessionParameters; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $agents = []; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(PriceFormatter $priceFormatter) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->priceFormatter = $priceFormatter; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Jeancsil\FlightSpy\Notifier\NotifiableInterface $notifier |
|
46
|
|
|
*/ |
|
47
|
|
|
public function addNotifier(NotifiableInterface $notifier) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->notifiers[] = $notifier; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Deal[] $deals |
|
54
|
|
|
*/ |
|
55
|
|
|
public function notifyAll(array $deals) |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($this->notifiers as $notifier) { |
|
58
|
|
|
$notifier->notify($deals, $this->sessionParameters); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $responses |
|
64
|
|
|
*/ |
|
65
|
|
|
public function multiProcess(array $responses) |
|
66
|
|
|
{ |
|
67
|
|
|
$deals = []; |
|
68
|
|
|
foreach ($responses as $response) { |
|
69
|
|
|
if (!$response) { |
|
70
|
|
|
$this->logger->warning(sprintf('Empty response received: %s', var_export($response, true))); |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$deals = array_merge($deals, $this->doProcess($response)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->notifyAll($deals); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \stdClass $response |
|
82
|
|
|
*/ |
|
83
|
|
|
public function singleProcess(\stdClass $response) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->notifyAll($this->doProcess($response)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param SessionParameters $sessionParameters |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setSessionParameters(SessionParameters $sessionParameters) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->sessionParameters = $sessionParameters; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param \stdClass $response |
|
101
|
|
|
* @return Deal[] |
|
102
|
|
|
*/ |
|
103
|
|
|
private function doProcess(\stdClass $response) |
|
104
|
|
|
{ |
|
105
|
|
|
$itineraries = $response->Itineraries; |
|
|
|
|
|
|
106
|
|
|
$this->agents = $response->Agents; |
|
107
|
|
|
|
|
108
|
|
|
$deals = []; |
|
|
|
|
|
|
109
|
|
|
$resultCount = 1; |
|
|
|
|
|
|
110
|
|
|
$cheapestItineraries = array_slice($itineraries, 0, static::MAX_PARSED_DEALS); |
|
111
|
|
|
foreach ($cheapestItineraries as $itinerary) { |
|
112
|
|
|
$logMessage = sprintf("Verifying itinerary #%s: ", $resultCount++); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
foreach ($itinerary['PricingOptions'] as $pricingOption) { |
|
115
|
|
|
$price = $this->getPrice($pricingOption); |
|
116
|
|
|
|
|
117
|
|
|
if ($price <= $this->sessionParameters->getMaxPrice()) { |
|
118
|
|
|
$formattedPrice = $this->priceFormatter->format($price, $this->sessionParameters->currency); |
|
119
|
|
|
$this->logger->debug( |
|
120
|
|
|
"$logMessage [{$this->getAgentName($pricingOption)}] Deal found ($formattedPrice)" |
|
|
|
|
|
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$deals[] = new Deal( |
|
124
|
|
|
$this->sessionParameters, |
|
125
|
|
|
$price, |
|
126
|
|
|
$this->getAgentName($pricingOption), |
|
127
|
|
|
$this->getDeepLinkUrl($pricingOption) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $deals; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param \stdClass $pricingOptions |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getDeepLinkUrl($pricingOptions) |
|
141
|
|
|
{ |
|
142
|
|
|
if (isset($pricingOptions['DeeplinkUrl'])) { |
|
143
|
|
|
return $pricingOptions['DeeplinkUrl']; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param \stdClass $pricingOptions |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
private function getPrice($pricingOptions) |
|
152
|
|
|
{ |
|
153
|
|
|
return $pricingOptions['Price']; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param \stdClass $pricingOptions |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getAgentName($pricingOptions) |
|
161
|
|
|
{ |
|
162
|
|
|
$agentId = 0; |
|
163
|
|
|
$agents = $pricingOptions['Agents']; |
|
|
|
|
|
|
164
|
|
|
foreach ($agents as $agent) { |
|
165
|
|
|
$agentId = $agent; |
|
166
|
|
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
foreach ($this->agents as $agent) { |
|
170
|
|
|
if ($agent['Id'] == $agentId) { |
|
171
|
|
|
return $agent['Name']; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
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.