1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VasilDakov\Shipping\Adapter; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use Laminas\Diactoros\RequestFactory; |
9
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
10
|
|
|
use VasilDakov\Econt\Configuration; |
|
|
|
|
11
|
|
|
use VasilDakov\Econt\Econt; |
12
|
|
|
use VasilDakov\Econt\EcontInterface; |
13
|
|
|
use VasilDakov\Econt\Request\GetCitiesRequest; |
|
|
|
|
14
|
|
|
use VasilDakov\Shipping\Model\City; |
15
|
|
|
use VasilDakov\Shipping\Model\Country; |
16
|
|
|
use VasilDakov\Shipping\Request; |
17
|
|
|
use VasilDakov\Shipping\Response; |
18
|
|
|
use Selective\Transformer\ArrayTransformer; |
19
|
|
|
|
20
|
|
|
use function array_filter; |
21
|
|
|
use function str_starts_with; |
22
|
|
|
|
23
|
|
|
final class EcontAdapter implements AdapterInterface |
24
|
|
|
{ |
25
|
|
|
private const NAME = 'Econt'; |
26
|
|
|
|
27
|
|
|
private ?EcontInterface $client; |
28
|
|
|
|
29
|
9 |
|
public function __construct(?EcontInterface $client = null) |
30
|
|
|
{ |
31
|
9 |
|
if (! $client) { |
32
|
4 |
|
$configuration = new Configuration( |
33
|
4 |
|
username: $_ENV['ECONT_USERNAME'], |
34
|
4 |
|
password: $_ENV['ECONT_PASSWORD'], |
35
|
4 |
|
); |
36
|
|
|
|
37
|
4 |
|
$client = new Econt( |
38
|
4 |
|
$configuration, |
39
|
4 |
|
new Client( |
40
|
4 |
|
[ |
41
|
4 |
|
'connect_timeout' => 5, |
42
|
4 |
|
'read_timeout' => 10, |
43
|
4 |
|
'verify' => false, |
44
|
4 |
|
] |
45
|
4 |
|
), |
46
|
4 |
|
new RequestFactory() |
47
|
4 |
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
$this->client = $client; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
5 |
|
private function jsonDecode(string $json): array |
55
|
|
|
{ |
56
|
5 |
|
return json_decode($json, true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws ClientExceptionInterface |
61
|
|
|
*/ |
62
|
|
|
public function getClientProfiles(): string |
63
|
|
|
{ |
64
|
|
|
return $this->client->getClientProfiles(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Request\GetCountriesRequest $request |
69
|
|
|
* @return Response\GetCountriesResponse |
70
|
|
|
* @throws ClientExceptionInterface |
71
|
|
|
*/ |
72
|
3 |
|
public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse |
73
|
|
|
{ |
74
|
3 |
|
$json = $this->client->getCountries(); |
75
|
3 |
|
$data = $this->jsonDecode($json); |
76
|
|
|
|
77
|
|
|
// transform properties to Shipping Model |
78
|
3 |
|
$transformer = new ArrayTransformer(); |
79
|
3 |
|
$transformer |
80
|
3 |
|
->map('id', 'id') |
81
|
3 |
|
->map('name', 'name') |
82
|
3 |
|
->map('nameEn', 'nameEn') |
83
|
3 |
|
->map('isoAlpha2', 'code2') |
84
|
3 |
|
->map('isoAlpha3', 'code3') |
85
|
3 |
|
; |
86
|
3 |
|
$result['countries'] = $transformer->toArrays($data['countries']); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// $hydrator = new \Laminas\Hydrator\ClassMethodsHydrator(); |
89
|
|
|
// $hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator(); |
90
|
|
|
// $hydrator = new \Laminas\Hydrator\ReflectionHydrator(); |
91
|
|
|
|
92
|
3 |
|
$strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy( |
93
|
3 |
|
new \Laminas\Hydrator\ObjectPropertyHydrator(), |
94
|
3 |
|
Country::class |
95
|
3 |
|
); |
96
|
3 |
|
$array = $strategy->hydrate($result['countries']); |
97
|
|
|
|
98
|
|
|
// implementing missing econt non-strict country search |
99
|
3 |
|
if (null !== $request->name) { |
100
|
1 |
|
$array = array_filter($array, function (Country $country) use ($request) { |
101
|
1 |
|
return |
102
|
1 |
|
str_starts_with($country->name, $request->name) || |
|
|
|
|
103
|
1 |
|
str_starts_with($country->nameEn, $request->name) |
104
|
1 |
|
; |
105
|
1 |
|
}); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
return new Response\GetCountriesResponse($array); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Request\GetCitiesRequest $request |
114
|
|
|
* @return Response\GetCitiesResponse |
115
|
|
|
* @throws ClientExceptionInterface |
116
|
|
|
*/ |
117
|
2 |
|
public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse |
118
|
|
|
{ |
119
|
2 |
|
$json = $this->client->getCities(new GetCitiesRequest(countryCode: $request->isoAlpha3)); |
120
|
|
|
|
121
|
2 |
|
$data = $this->jsonDecode($json); |
122
|
|
|
|
123
|
|
|
// transform rules |
124
|
2 |
|
$transformer = new ArrayTransformer(); |
125
|
2 |
|
$transformer |
126
|
2 |
|
->map('id', 'id') |
127
|
2 |
|
->map('country', 'country') |
128
|
2 |
|
->map('postCode', 'postCode') |
129
|
2 |
|
->map('name', 'name') |
130
|
2 |
|
->map('nameEn', 'nameEn') |
131
|
2 |
|
; |
132
|
|
|
|
133
|
|
|
// transform array to shipping model |
134
|
2 |
|
$result['cities'] = $transformer->toArrays($data['cities']); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// hydrate the array |
137
|
2 |
|
$hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator(); |
138
|
2 |
|
$strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy( |
139
|
2 |
|
$hydrator, |
140
|
2 |
|
City::class |
141
|
2 |
|
); |
142
|
2 |
|
$hydrator->addStrategy( |
143
|
2 |
|
'country', |
144
|
2 |
|
new \Laminas\Hydrator\Strategy\HydratorStrategy( |
145
|
2 |
|
new \Laminas\Hydrator\ReflectionHydrator(), |
146
|
2 |
|
Country::class |
147
|
2 |
|
) |
148
|
2 |
|
); |
149
|
|
|
|
150
|
2 |
|
$array = $strategy->hydrate($result['cities']); |
151
|
|
|
|
152
|
2 |
|
if (null !== $request->name) { |
153
|
1 |
|
$array = array_filter($array, function (City $city) use ($request) { |
154
|
1 |
|
return |
155
|
1 |
|
str_starts_with($city->name, $request->name) || |
|
|
|
|
156
|
1 |
|
str_starts_with($city->nameEn, $request->name) |
157
|
1 |
|
; |
158
|
1 |
|
}); |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
return new Response\GetCitiesResponse($array); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
*/ |
166
|
|
|
public function getOffices(Request\GetOfficesRequest $request): Response\GetOfficesResponse |
167
|
|
|
{ |
168
|
|
|
//return $this->client->getOffices($data); |
169
|
|
|
return new Response\GetOfficesResponse(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function calculate() |
173
|
|
|
{ |
174
|
|
|
return $this->client->calculate(); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function track(array $data) |
178
|
|
|
{ |
179
|
|
|
return $this->client->getShipmentStatuses($data); |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
public function getName(): string |
183
|
|
|
{ |
184
|
1 |
|
return self::NAME; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths