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\Office\FindOfficeRequest; |
16
|
|
|
use VasilDakov\Speedy\Service\Location\Office\FindOfficeResponse; |
17
|
|
|
use VasilDakov\Speedy\Service\Location\Site\FindSiteRequest; |
18
|
|
|
use VasilDakov\Speedy\Service\Location\Site\FindSiteResponse; |
19
|
|
|
use VasilDakov\Speedy\Speedy; |
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use Laminas\Diactoros\RequestFactory; |
22
|
|
|
use VasilDakov\Shipping\Response; |
23
|
|
|
use VasilDakov\Shipping\Request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* SpeedyAdapter |
27
|
|
|
* |
28
|
|
|
* @author Vasil Dakov <[email protected]> |
29
|
|
|
* @copyright 2009-2024 Neutrino.bg |
30
|
|
|
* @version 1.0 |
31
|
|
|
*/ |
32
|
|
|
final class SpeedyAdapter implements AdapterInterface |
33
|
|
|
{ |
34
|
|
|
private const NAME = 'Speedy'; |
35
|
|
|
|
36
|
|
|
private ?Speedy $client; |
37
|
|
|
|
38
|
7 |
|
public function __construct(?Speedy $client = null) |
39
|
|
|
{ |
40
|
7 |
|
if (null === $client) { |
41
|
7 |
|
$client = new Speedy( |
42
|
7 |
|
new Configuration( |
43
|
7 |
|
username: $_ENV['SPEEDY_USERNAME'], |
44
|
7 |
|
password: $_ENV['SPEEDY_PASSWORD'], |
45
|
7 |
|
language: $_ENV['SPEEDY_LANGUAGE'] |
46
|
7 |
|
), |
47
|
7 |
|
new Client(), |
48
|
7 |
|
new RequestFactory() |
49
|
7 |
|
); |
50
|
|
|
} |
51
|
7 |
|
$this->client = $client; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
1 |
|
public function getName(): string |
58
|
|
|
{ |
59
|
1 |
|
return self::NAME; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param GetCountriesRequest $request |
64
|
|
|
* @return Response\GetCountriesResponse |
65
|
|
|
* @throws ClientExceptionInterface |
66
|
|
|
*/ |
67
|
1 |
|
public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse |
68
|
|
|
{ |
69
|
1 |
|
$json = $this->client->findCountry( |
|
|
|
|
70
|
1 |
|
new FindCountryRequest(name: 'Bulgaria') |
71
|
1 |
|
); |
72
|
1 |
|
$data = json_decode($json, true); |
73
|
|
|
|
74
|
1 |
|
$transformer = new ArrayTransformer(); |
75
|
1 |
|
$transformer |
76
|
1 |
|
->map('id', 'id') |
77
|
1 |
|
->map('name', 'name') |
78
|
1 |
|
->map('nameEn', 'nameEn') |
79
|
1 |
|
->map('isoAlpha2', 'isoAlpha2') |
80
|
1 |
|
->map('isoAlpha3', 'isoAlpha3') |
81
|
1 |
|
; |
82
|
|
|
|
83
|
1 |
|
$result['countries'] = $transformer->toArray($data); |
|
|
|
|
84
|
|
|
|
85
|
1 |
|
$strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy( |
86
|
1 |
|
new \Laminas\Hydrator\ObjectPropertyHydrator(), |
87
|
1 |
|
Country::class |
88
|
1 |
|
); |
89
|
1 |
|
$array = $strategy->hydrate($result['countries']); |
90
|
|
|
|
91
|
1 |
|
return new Response\GetCountriesResponse($array); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Request\GetCitiesRequest $request |
96
|
|
|
* @return Response\GetCitiesResponse |
97
|
|
|
* @throws ClientExceptionInterface |
98
|
|
|
*/ |
99
|
1 |
|
public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse |
100
|
|
|
{ |
101
|
1 |
|
$object = new FindSiteRequest( |
102
|
1 |
|
countryId: $request->countryId, |
|
|
|
|
103
|
1 |
|
name: $request->name |
104
|
1 |
|
); |
105
|
|
|
|
106
|
1 |
|
$json = $this->client->findSite($object); |
107
|
1 |
|
$data = $this->jsonDecode($json); |
108
|
|
|
|
109
|
1 |
|
$transformer = new ArrayTransformer(); |
110
|
1 |
|
$transformer |
111
|
1 |
|
->map('id', 'id') |
112
|
1 |
|
->map('countryId', 'countryId') |
113
|
1 |
|
->map('name', 'name') |
114
|
1 |
|
->map('nameEn', 'nameEn') |
115
|
1 |
|
->map('postCode', 'postCode') |
116
|
1 |
|
; |
117
|
|
|
|
118
|
1 |
|
$result['cities'] = $transformer->toArrays($data['sites']); |
|
|
|
|
119
|
|
|
|
120
|
1 |
|
return new Response\GetCitiesResponse($result); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function getOffices(Request\GetOfficesRequest $request): Response\GetOfficesResponse |
124
|
|
|
{ |
125
|
1 |
|
$object = new FindOfficeRequest( |
126
|
1 |
|
siteId: $request->cityId |
|
|
|
|
127
|
1 |
|
); |
128
|
|
|
|
129
|
1 |
|
$json = $this->client->findOffice($object); |
130
|
1 |
|
$data = $this->jsonDecode($json); |
131
|
|
|
|
132
|
1 |
|
$transformer = new ArrayTransformer(); |
133
|
1 |
|
$transformer |
134
|
1 |
|
->map('id', 'id') |
135
|
1 |
|
->map('name', 'name') |
136
|
1 |
|
->map('nameEn', 'nameEn') |
137
|
1 |
|
; |
138
|
|
|
|
139
|
1 |
|
$result['offices'] = $transformer->toArrays($data['offices']); |
|
|
|
|
140
|
|
|
|
141
|
1 |
|
return new Response\GetOfficesResponse($result); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function track(array $data) |
145
|
|
|
{ |
146
|
|
|
// TODO: Implement track() method. |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
private function jsonDecode(string $json): array |
150
|
|
|
{ |
151
|
2 |
|
return json_decode($json, true); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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