|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Jean Silva <[email protected]> |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Jeancsil\FlightSpy\Api\Http; |
|
7
|
|
|
|
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
10
|
|
|
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters; |
|
11
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchWriterTrait; |
|
12
|
|
|
use Jeancsil\FlightSpy\History\ElasticSearch\MappingProcessor; |
|
13
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
class Transport implements TransportInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use LoggerAwareTrait; |
|
18
|
|
|
use ElasticSearchWriterTrait; |
|
19
|
|
|
|
|
20
|
|
|
const LIVE_PRICING = '/apiservices/pricing/v1.0'; |
|
|
|
|
|
|
21
|
|
|
const WAITING_TIME_IN_MILLIS = 500000; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \GuzzleHttp\Client |
|
25
|
|
|
*/ |
|
26
|
|
|
private $client; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $pollUrl; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ClientInterface $client |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(ClientInterface $client) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->client = $client; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param SessionParameters $parameters |
|
43
|
|
|
* @param bool $newSession |
|
44
|
|
|
* @return \stdClass |
|
45
|
|
|
*/ |
|
46
|
|
|
public function findQuotes(SessionParameters $parameters, $newSession) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($newSession) { |
|
49
|
|
|
$this->createSession($parameters); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->poll(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param SessionParameters $parameters |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
private function createSession(SessionParameters $parameters) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$parametersArray = $parameters->toArray(); |
|
63
|
|
|
|
|
64
|
|
|
$request = $this |
|
65
|
|
|
->client |
|
66
|
|
|
->post(static::LIVE_PRICING, ['form_params' => $parametersArray]); |
|
67
|
|
|
|
|
68
|
|
|
if (!isset($request->getHeaders()['Location'][0])) { |
|
69
|
|
|
throw new \Exception('Location not found for the poll'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->setPollUrl($request->getHeaders()['Location'][0], $parametersArray['apiKey']); |
|
73
|
|
|
usleep(static::WAITING_TIME_IN_MILLIS); |
|
74
|
|
|
} catch (BadResponseException $e) { |
|
75
|
|
|
$this->logger->error( |
|
76
|
|
|
sprintf( |
|
77
|
|
|
"[Transport::createSession]\nStatusCode: %d\nBody: %s", |
|
78
|
|
|
$e->getResponse()->getStatusCode(), |
|
79
|
|
|
$e->getResponse()->getBody() |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
throw $e; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \stdClass |
|
89
|
|
|
*/ |
|
90
|
|
|
private function poll() |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
|
|
$response = $this |
|
94
|
|
|
->client |
|
95
|
|
|
->get($this->pollUrl); |
|
96
|
|
|
|
|
97
|
|
|
$this->logger->info("HTTPStatusCode: " . $response->getStatusCode()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if ($response->getStatusCode() != 200) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$arrayContent = \GuzzleHttp\json_decode( |
|
104
|
|
|
str_replace("'", '\'', $response->getBody()->getContents()), |
|
105
|
|
|
true |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
if (!is_array($arrayContent)) { |
|
109
|
|
|
$this->logger->critical( |
|
110
|
|
|
sprintf('Expecting $arrayContent to be an array. %s given.', gettype($arrayContent)) |
|
111
|
|
|
); |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->getElasticSearchWriter() |
|
116
|
|
|
->setProcessor(new MappingProcessor()) |
|
117
|
|
|
->write($arrayContent); |
|
118
|
|
|
|
|
119
|
|
|
return (object) $arrayContent; |
|
120
|
|
|
} catch (BadResponseException $e) { |
|
121
|
|
|
$this->logger->error( |
|
122
|
|
|
sprintf( |
|
123
|
|
|
"[Transport::pool]\nStatusCode: %d\nBody: %s", |
|
124
|
|
|
$e->getResponse()->getStatusCode(), |
|
125
|
|
|
$e->getResponse()->getBody() |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
} catch (\Exception $e) { |
|
129
|
|
|
$this->logger->error( |
|
130
|
|
|
sprintf( |
|
131
|
|
|
"[Transport::pool]\nException:\n%s", |
|
132
|
|
|
$e->getMessage() |
|
133
|
|
|
) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $url |
|
140
|
|
|
* @param string $apiKey |
|
141
|
|
|
*/ |
|
142
|
|
|
private function setPollUrl($url, $apiKey) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->pollUrl = "$url?apiKey=$apiKey&sorttype=price&sortorder=asc"; |
|
|
|
|
|
|
145
|
|
|
$this->logger->debug("Pool url is: $this->pollUrl"); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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.