1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jean Silva <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Jeancsil\FlightSpy\Api\DataTransfer; |
7
|
|
|
|
8
|
|
|
use Jeancsil\FlightSpy\Command\Entity\Parameter; |
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
|
12
|
|
|
class SessionParametersFactory |
13
|
|
|
{ |
14
|
|
|
use LoggerAwareTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $apiKey; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $configCache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $apiKey |
28
|
|
|
*/ |
29
|
|
|
public function __construct($apiKey) |
30
|
|
|
{ |
31
|
|
|
$this->apiKey = $apiKey; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $configFile |
36
|
|
|
* @return SessionParameters[] |
37
|
|
|
*/ |
38
|
|
|
public function createFromConfigFile($configFile) |
39
|
|
|
{ |
40
|
|
|
$configurations = json_decode(file_get_contents($configFile), true); |
41
|
|
|
|
42
|
|
|
$parameters = []; |
43
|
|
|
$maxPrices = []; |
|
|
|
|
44
|
|
|
foreach ($configurations as $configuration) { |
45
|
|
|
$parameters = $this->createFromArray($configuration); |
|
|
|
|
46
|
|
|
$maxPrices[] = $this->getValue(Parameter::MAX_PRICE); |
47
|
|
|
$this->logger->debug($parameters); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $parameters; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param InputInterface $input |
55
|
|
|
* @return SessionParameters |
56
|
|
|
*/ |
57
|
|
|
public function createFromInput(InputInterface $input) |
58
|
|
|
{ |
59
|
|
|
$parameters = new SessionParameters(); |
|
|
|
|
60
|
|
|
$parameters->apiKey = $input->getOption(Parameter::API_KEY) ?: $this->apiKey; |
|
|
|
|
61
|
|
|
$parameters->originPlace = $input->getOption(Parameter::FROM); |
|
|
|
|
62
|
|
|
$parameters->destinationPlace = $input->getOption(Parameter::TO); |
63
|
|
|
$parameters->outboundDate = $input->getOption(Parameter::DEPARTURE_DATE); |
|
|
|
|
64
|
|
|
$parameters->inboundDate = $input->getOption(Parameter::RETURN_DATE); |
|
|
|
|
65
|
|
|
$parameters->locationSchema = $input->getOption(Parameter::LOCATION_SCHEMA); |
|
|
|
|
66
|
|
|
$parameters->country = $input->getOption(Parameter::COUNTRY); |
|
|
|
|
67
|
|
|
$parameters->currency = $input->getOption(Parameter::CURRENCY); |
|
|
|
|
68
|
|
|
$parameters->locale = $input->getOption(Parameter::LOCALE); |
|
|
|
|
69
|
|
|
$parameters->adults = $input->getOption(Parameter::ADULTS); |
|
|
|
|
70
|
|
|
$parameters->cabinClass = $input->getOption(Parameter::CABIN_CLASS); |
|
|
|
|
71
|
|
|
$parameters->children = $input->getOption(Parameter::CHILDREN); |
|
|
|
|
72
|
|
|
$parameters->infants = $input->getOption(Parameter::INFANTS); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $parameters; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $configuration |
79
|
|
|
* @return SessionParameters[] |
80
|
|
|
*/ |
81
|
|
|
private function createFromArray(array $configuration) |
82
|
|
|
{ |
83
|
|
|
$this->configCache = $configuration; |
84
|
|
|
$parameters = []; |
|
|
|
|
85
|
|
|
|
86
|
|
|
if ($this->shouldCreateAllPossiblePeriods()) { |
87
|
|
|
$dateCombinations = $this->getPeriod()->generateDateCombinations(); |
88
|
|
|
|
89
|
|
|
foreach ($dateCombinations as $combination) { |
|
|
|
|
90
|
|
|
$parameters[] = $this->createSessionParameters( |
91
|
|
|
$combination['outboundDate'], |
92
|
|
|
$combination['inboundDate'] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $parameters; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$parameters[] = $this->createSessionParameters( |
100
|
|
|
$this->getValue(Parameter::DEPARTURE_DATE), |
101
|
|
|
$this->getValue(Parameter::RETURN_DATE) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $parameters; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $outboundDate |
109
|
|
|
* @param $inboundDate |
110
|
|
|
* @return SessionParameters |
111
|
|
|
*/ |
112
|
|
|
private function createSessionParameters($outboundDate, $inboundDate) |
113
|
|
|
{ |
114
|
|
|
$parameters = new SessionParameters(); |
115
|
|
|
$parameters->setMaxPrice($this->getValue(Parameter::MAX_PRICE)); |
116
|
|
|
$parameters->apiKey = $this->getValue(Parameter::API_KEY, $this->apiKey); |
|
|
|
|
117
|
|
|
$parameters->originPlace = $this->getValue(Parameter::FROM); |
|
|
|
|
118
|
|
|
$parameters->destinationPlace = $this->getValue(Parameter::TO); |
119
|
|
|
$parameters->outboundDate = $outboundDate; |
|
|
|
|
120
|
|
|
$parameters->inboundDate = $inboundDate; |
|
|
|
|
121
|
|
|
$parameters->locationSchema = $this->getValue(Parameter::LOCATION_SCHEMA, Parameter::DEFAULT_LOCATION_SCHEMA); |
|
|
|
|
122
|
|
|
$parameters->country = $this->getValue(Parameter::COUNTRY); |
|
|
|
|
123
|
|
|
$parameters->currency = $this->getValue(Parameter::CURRENCY); |
|
|
|
|
124
|
|
|
$parameters->locale = $this->getValue(Parameter::LOCALE); |
|
|
|
|
125
|
|
|
$parameters->adults = $this->getValue(Parameter::ADULTS, Parameter::DEFAULT_ADULTS); |
|
|
|
|
126
|
|
|
$parameters->cabinClass = $this->getValue(Parameter::CABIN_CLASS, Parameter::DEFAULT_CABIN_CLASS); |
|
|
|
|
127
|
|
|
$parameters->children = $this->getValue(Parameter::CHILDREN, Parameter::DEFAULT_CHILDREN); |
|
|
|
|
128
|
|
|
$parameters->infants = $this->getValue(Parameter::INFANTS, Parameter::DEFAULT_INFANTS); |
|
|
|
|
129
|
|
|
|
130
|
|
|
return $parameters; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Period |
135
|
|
|
*/ |
136
|
|
|
private function getPeriod() |
137
|
|
|
{ |
138
|
|
|
return new Period( |
139
|
|
|
$this->configCache[Parameter::SEARCH_PERIOD_TRAVEL_DAYS], |
140
|
|
|
new \DateTime($this->configCache[Parameter::SEARCH_PERIOD_FROM]), |
141
|
|
|
new \DateTime($this->configCache[Parameter::SEARCH_PERIOD_TO]) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
private function shouldCreateAllPossiblePeriods() |
149
|
|
|
{ |
150
|
|
|
if (!isset($this->configCache[Parameter::SEARCH_PERIOD_FROM])) { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!isset($this->configCache[Parameter::SEARCH_PERIOD_TO])) { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!isset($this->configCache[Parameter::SEARCH_PERIOD_TRAVEL_DAYS])) { |
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $parameter |
167
|
|
|
* @param mixed $defaultValue |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
private function getValue($parameter, $defaultValue = null) |
171
|
|
|
{ |
172
|
|
|
if (isset($this->configCache[$parameter])) { |
173
|
|
|
return $this->configCache[$parameter]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $defaultValue; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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.