1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VasilDakov\Shipping\Adapter; |
6
|
|
|
|
7
|
|
|
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection; |
|
|
|
|
8
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
9
|
|
|
use Selective\Transformer\ArrayTransformer; |
10
|
|
|
use VasilDakov\Shipping\Model\City; |
11
|
|
|
use VasilDakov\Shipping\Model\Country; |
12
|
|
|
use VasilDakov\Shipping\Request\GetCountriesRequest; |
13
|
|
|
use VasilDakov\Speedy\Configuration; |
14
|
|
|
use VasilDakov\Speedy\Service\Location\Country\FindCountryRequest; |
15
|
|
|
use VasilDakov\Speedy\Service\Location\Site\FindSiteRequest; |
16
|
|
|
use VasilDakov\Speedy\Service\Location\Site\FindSiteResponse; |
17
|
|
|
use VasilDakov\Speedy\Speedy; |
18
|
|
|
use GuzzleHttp\Client; |
19
|
|
|
use Laminas\Diactoros\RequestFactory; |
20
|
|
|
use VasilDakov\Shipping\Response; |
21
|
|
|
use VasilDakov\Shipping\Request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SpeedyAdapter |
25
|
|
|
* |
26
|
|
|
* @author Vasil Dakov <[email protected]> |
27
|
|
|
* @copyright 2009-2024 Neutrino.bg |
28
|
|
|
* @version 1.0 |
29
|
|
|
*/ |
30
|
|
|
final class SpeedyAdapter implements AdapterInterface |
31
|
|
|
{ |
32
|
|
|
private const NAME = 'Speedy'; |
33
|
|
|
|
34
|
|
|
private ?Speedy $client; |
35
|
|
|
|
36
|
4 |
|
public function __construct(?Speedy $client = null) |
37
|
|
|
{ |
38
|
4 |
|
if (null === $client) { |
39
|
4 |
|
$client = new Speedy( |
40
|
4 |
|
new Configuration( |
41
|
4 |
|
username: $_ENV['SPEEDY_USERNAME'], |
42
|
4 |
|
password: $_ENV['SPEEDY_PASSWORD'], |
43
|
4 |
|
language: $_ENV['SPEEDY_LANGUAGE'] |
44
|
4 |
|
), |
45
|
4 |
|
new Client(), |
46
|
4 |
|
new RequestFactory() |
47
|
4 |
|
); |
48
|
|
|
} |
49
|
4 |
|
$this->client = $client; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function getName(): string |
56
|
|
|
{ |
57
|
1 |
|
return self::NAME; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param GetCountriesRequest $request |
62
|
|
|
* @return Response\GetCountriesResponse |
63
|
|
|
* @throws ClientExceptionInterface |
64
|
|
|
*/ |
65
|
|
|
public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse |
66
|
|
|
{ |
67
|
|
|
$json = $this->client->findCountry( |
|
|
|
|
68
|
|
|
new FindCountryRequest(name: $request->name) |
|
|
|
|
69
|
|
|
); |
70
|
|
|
$data = json_decode($json, true); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$transformer = new ArrayTransformer(); |
73
|
|
|
$transformer |
74
|
|
|
->map('id', 'id') |
75
|
|
|
->map('name', 'name') |
76
|
|
|
->map('nameEn', 'nameEn') |
77
|
|
|
->map('isoAlpha2', 'isoAlpha2') |
78
|
|
|
->map('isoAlpha3', 'isoAlpha3') |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
$result = []; |
82
|
|
|
foreach ($data['countries'] as $country) { |
83
|
|
|
$result['countries'][] = $transformer->toArray($country); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy( |
87
|
|
|
new \Laminas\Hydrator\ObjectPropertyHydrator(), |
88
|
|
|
Country::class |
89
|
|
|
); |
90
|
|
|
$array = $strategy->hydrate($result['countries']); |
91
|
|
|
|
92
|
|
|
return new Response\GetCountriesResponse($array); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Request\GetCitiesRequest $request |
97
|
|
|
* @return Response\GetCitiesResponse |
98
|
|
|
* @throws ClientExceptionInterface |
99
|
|
|
*/ |
100
|
|
|
public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse |
101
|
|
|
{ |
102
|
|
|
$object = new FindSiteRequest( |
103
|
|
|
countryId: $request->countryId, |
|
|
|
|
104
|
|
|
name: $request->name |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$result = $this->client->findSite($object); |
108
|
|
|
|
109
|
|
|
$transformer = new ArrayTransformer(); |
110
|
|
|
$transformer |
111
|
|
|
->map('id', 'id') |
112
|
|
|
->map('countryId', 'countryId') |
113
|
|
|
->map('name', 'name') |
114
|
|
|
->map('nameEn', 'nameEn') |
115
|
|
|
->map('postCode', 'postCode') |
116
|
|
|
; |
117
|
|
|
|
118
|
|
|
$records = []; |
119
|
|
|
foreach ($result->getSites() as $site) { |
120
|
|
|
$records['cities'][] = $transformer->toArray($site->toArray()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new Response\GetCitiesResponse($records); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getOffices(array $data) |
127
|
|
|
{ |
128
|
|
|
// TODO: Implement getOffices() method. |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function track(array $data) |
132
|
|
|
{ |
133
|
|
|
// TODO: Implement track() method. |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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