1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VasilDakov\Shipping\Adapter; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Handler\CurlFactory; |
9
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
10
|
|
|
use Laminas\Diactoros\RequestFactory; |
11
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
12
|
|
|
use VasilDakov\Econt\Configuration; |
|
|
|
|
13
|
|
|
use VasilDakov\Econt\Econt; |
14
|
|
|
use VasilDakov\Econt\EcontInterface; |
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
|
|
|
class EcontAdapter implements AdapterInterface |
24
|
|
|
{ |
25
|
|
|
private const NAME = 'Econt'; |
26
|
|
|
|
27
|
|
|
private ?EcontInterface $client; |
28
|
|
|
|
29
|
4 |
|
public function __construct(?EcontInterface $client = null) |
30
|
|
|
{ |
31
|
|
|
|
32
|
4 |
|
if (! $client) { |
33
|
1 |
|
$configuration = new Configuration( |
34
|
1 |
|
username: $_ENV['ECONT_USERNAME'], |
35
|
1 |
|
password: $_ENV['ECONT_PASSWORD'], |
36
|
1 |
|
); |
37
|
|
|
|
38
|
1 |
|
$client = new Econt( |
39
|
1 |
|
$configuration, |
40
|
1 |
|
new Client( |
41
|
1 |
|
[ |
42
|
1 |
|
'connect_timeout' => 5, |
43
|
1 |
|
'read_timeout' => 10, |
44
|
1 |
|
'verify' => false, |
45
|
1 |
|
] |
46
|
1 |
|
), |
47
|
1 |
|
new RequestFactory() |
48
|
1 |
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
$this->client = $client; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws ClientExceptionInterface |
56
|
|
|
*/ |
57
|
|
|
public function getClientProfiles(): string |
58
|
|
|
{ |
59
|
|
|
return $this->client->getClientProfiles(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Request\GetCountriesRequest $request |
65
|
|
|
* @return Response\GetCountriesResponse |
66
|
|
|
* @throws ClientExceptionInterface |
67
|
|
|
*/ |
68
|
3 |
|
public function getCountries( |
69
|
|
|
Request\GetCountriesRequest $request |
70
|
|
|
): Response\GetCountriesResponse |
71
|
|
|
{ |
72
|
3 |
|
$json = $this->client->getCountries(); |
73
|
3 |
|
$data = json_decode($json, true); |
74
|
|
|
|
75
|
|
|
// transform properties to Shipping Model |
76
|
3 |
|
$transformer = new ArrayTransformer(); |
77
|
3 |
|
$transformer |
78
|
3 |
|
->map('id', 'id') |
79
|
3 |
|
->map('name', 'name') |
80
|
3 |
|
->map('nameEn', 'nameEn') |
81
|
3 |
|
->map('isoAlpha2', 'code2') |
82
|
3 |
|
->map('isoAlpha3', 'code3') |
83
|
3 |
|
; |
84
|
3 |
|
$result = []; |
85
|
3 |
|
foreach ($data['countries'] as $country) { |
86
|
3 |
|
$result['countries'][] = $transformer->toArray($country); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// $hydrator = new \Laminas\Hydrator\ClassMethodsHydrator(); |
90
|
|
|
// $hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator(); |
91
|
|
|
// $hydrator = new \Laminas\Hydrator\ReflectionHydrator(); |
92
|
|
|
|
93
|
3 |
|
$strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy( |
94
|
3 |
|
new \Laminas\Hydrator\ObjectPropertyHydrator(), |
95
|
3 |
|
Country::class |
96
|
3 |
|
); |
97
|
3 |
|
$array = $strategy->hydrate($result['countries']); |
98
|
|
|
|
99
|
|
|
// implementing missing econt non-strict country search |
100
|
3 |
|
if (null !== $request->name) { |
101
|
1 |
|
$array = array_filter($array, function (Country $country) use ($request) { |
102
|
1 |
|
return |
103
|
1 |
|
str_starts_with($country->name, $request->name) || |
|
|
|
|
104
|
1 |
|
str_starts_with($country->nameEn, $request->name) |
105
|
1 |
|
; |
106
|
1 |
|
}); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
return new Response\GetCountriesResponse($array); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @throws ClientExceptionInterface |
115
|
|
|
*/ |
116
|
|
|
public function getCities(array $data): string |
117
|
|
|
{ |
118
|
|
|
return $this->client->getCities($data); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
*/ |
123
|
|
|
public function getOffices(array $data): string |
124
|
|
|
{ |
125
|
|
|
return $this->client->getOffices($data); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function calculate() |
129
|
|
|
{ |
130
|
|
|
return $this->client->calculate(); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function track(array $data) |
134
|
|
|
{ |
135
|
|
|
return $this->client->getShipmentStatuses($data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getName(): string |
139
|
|
|
{ |
140
|
|
|
return self::NAME; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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