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