|
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 Psr\Log\LoggerAwareTrait; |
|
12
|
|
|
|
|
13
|
|
|
class Transport |
|
14
|
|
|
{ |
|
15
|
|
|
use LoggerAwareTrait; |
|
16
|
|
|
|
|
17
|
|
|
const LIVE_PRICING = '/apiservices/pricing/v1.0'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \GuzzleHttp\Client |
|
21
|
|
|
*/ |
|
22
|
|
|
private $client; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $pollUrl; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ClientInterface $client |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(ClientInterface $client) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->client = $client; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param SessionParameters $parameters |
|
39
|
|
|
* @param bool $newSession |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function findQuotes(SessionParameters $parameters, $newSession) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($newSession) { |
|
45
|
|
|
$this->createSession($parameters); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->poll(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param SessionParameters $parameters |
|
53
|
|
|
* @throws \Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
private function createSession(SessionParameters $parameters) |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
$parametersArray = $parameters->toArray(); |
|
59
|
|
|
|
|
60
|
|
|
$request = $this |
|
61
|
|
|
->client |
|
62
|
|
|
->post(static::LIVE_PRICING, ['form_params' => $parametersArray]); |
|
63
|
|
|
|
|
64
|
|
|
if (!isset($request->getHeaders()['Location'][0])) { |
|
65
|
|
|
throw new \Exception('Location not found for the poll'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->setPollUrl($request->getHeaders()['Location'][0], $parametersArray['apiKey']); |
|
69
|
|
|
sleep(1); |
|
70
|
|
|
} catch (BadResponseException $e) { |
|
71
|
|
|
$this->logger->error( |
|
72
|
|
|
sprintf( |
|
73
|
|
|
"[Transport::createSession]\nStatusCode: %d\nBody: %s", |
|
74
|
|
|
$e->getResponse()->getStatusCode(), |
|
75
|
|
|
$e->getResponse()->getBody() |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
throw $e; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
private function poll() |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
$request = $this |
|
90
|
|
|
->client |
|
91
|
|
|
->get($this->pollUrl); |
|
92
|
|
|
|
|
93
|
|
|
return \GuzzleHttp\json_decode($request->getBody()->getContents()); |
|
94
|
|
|
} catch (BadResponseException $e) { |
|
95
|
|
|
$this->logger->error( |
|
96
|
|
|
sprintf( |
|
97
|
|
|
"[Transport::pool]\nStatusCode: %d\nBody: %s", |
|
98
|
|
|
$e->getResponse()->getStatusCode(), |
|
99
|
|
|
$e->getResponse()->getBody() |
|
100
|
|
|
) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $url |
|
107
|
|
|
* @param string $apiKey |
|
108
|
|
|
*/ |
|
109
|
|
|
private function setPollUrl($url, $apiKey) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->pollUrl = "$url?apiKey=$apiKey&sorttype=price&sortorder=desc"; |
|
|
|
|
|
|
112
|
|
|
$this->logger->debug("Pool url is: $this->pollUrl"); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.