1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Rest;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Temando\Shipping\Rest\Adapter\BatchApiInterface;
use Temando\Shipping\Rest\Adapter\ShipmentApiInterface;
use Temando\Shipping\Rest\Exception\AdapterException;
use Temando\Shipping\Rest\Exception\RestClientErrorException;
use Temando\Shipping\Rest\Request\ItemRequestInterface;
use Temando\Shipping\Rest\Request\RequestHeadersInterface;
use Temando\Shipping\Rest\Response\DataObject\Batch;
use Temando\Shipping\Rest\Response\DataObject\Shipment;
use Temando\Shipping\Rest\Response\Document\Errors;
use Temando\Shipping\Rest\Response\Document\GetBatch;
use Temando\Shipping\Rest\Response\Document\GetShipment;
use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
use Temando\Shipping\Webservice\Config\WsConfigInterface;
/**
* Temando REST API Shipment Operations Adapter
*
* @package Temando\Shipping\Rest
* @author Christoph Aßmann <christoph.assmann@netresearch.de>
* @author Sebastian Ertner <sebastian.ertner@netresearch.de>
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.temando.com/
*/
class ShipmentAdapter implements BatchApiInterface, ShipmentApiInterface
{
/**
* @var string
*/
private $endpoint;
/**
* @var string
*/
private $accountId;
/**
* @var string
*/
private $bearerToken;
/**
* @var RequestHeadersInterface
*/
private $requestHeaders;
/**
* @var AuthenticationInterface
*/
private $auth;
/**
* @var RestClientInterface
*/
private $restClient;
/**
* @var ParserInterface
*/
private $responseParser;
/**
* @var LoggerInterface
*/
private $logger;
/**
* ShipmentAdapter constructor.
* @param WsConfigInterface $config
* @param RequestHeadersInterface $requestHeaders
* @param AuthenticationInterface $auth,
* @param RestClientInterface $restClient
* @param ParserInterface $responseParser
* @param LoggerInterface $logger
*/
public function __construct(
WsConfigInterface $config,
RequestHeadersInterface $requestHeaders,
AuthenticationInterface $auth,
RestClientInterface $restClient,
ParserInterface $responseParser,
LoggerInterface $logger
) {
$this->endpoint = $config->getApiEndpoint();
$this->accountId = $config->getAccountId();
$this->bearerToken = $config->getBearerToken();
$this->requestHeaders = $requestHeaders;
$this->auth = $auth;
$this->restClient = $restClient;
$this->responseParser = $responseParser;
$this->logger = $logger;
}
/**
* Read a batch from the platform.
*
* @param ItemRequestInterface $request
* @return Batch
* @throws AdapterException
*/
public function getBatch(ItemRequestInterface $request)
{
$uri = sprintf('%s/shipments/batches/%s', $this->endpoint, ...$request->getPathParams());
$this->logger->log(LogLevel::DEBUG, $uri);
try {
$this->auth->connect($this->accountId, $this->bearerToken);
$headers = $this->requestHeaders->getHeaders();
$rawResponse = $this->restClient->get($uri, [], $headers);
$this->logger->log(LogLevel::DEBUG, $rawResponse);
/** @var GetBatch $response */
$response = $this->responseParser->parse($rawResponse, GetBatch::class);
$batch = $response->getData();
$batch->setShipments($response->getIncluded());
} catch (RestClientErrorException $e) {
$this->logger->log(LogLevel::ERROR, $e->getMessage());
/** @var Errors $response */
$response = $this->responseParser->parse($e->getMessage(), Errors::class);
throw AdapterException::errorResponse($response, $e);
} catch (\Exception $e) {
throw AdapterException::create($e);
}
return $batch;
}
/**
* Read one shipment from the platform.
*
* @param ItemRequestInterface $request
* @return Shipment
* @throws AdapterException
*/
public function getShipment(ItemRequestInterface $request)
{
$uri = sprintf('%s/shipments/%s', $this->endpoint, ...$request->getPathParams());
$this->logger->log(LogLevel::DEBUG, $uri);
try {
$this->auth->connect($this->accountId, $this->bearerToken);
$headers = $this->requestHeaders->getHeaders();
$rawResponse = $this->restClient->get($uri, [], $headers);
$this->logger->log(LogLevel::DEBUG, $rawResponse);
/** @var GetShipment $response */
$response = $this->responseParser->parse($rawResponse, GetShipment::class);
$shipment = $response->getData();
} catch (RestClientErrorException $e) {
$this->logger->log(LogLevel::ERROR, $e->getMessage());
/** @var Errors $response */
$response = $this->responseParser->parse($e->getMessage(), Errors::class);
throw AdapterException::errorResponse($response, $e);
} catch (\Exception $e) {
throw AdapterException::create($e);
}
return $shipment;
}
/**
* Cancel shipment at the platform.
*
* @param ItemRequestInterface $request
* @return Shipment
* @throws AdapterException
*/
public function cancelShipment(ItemRequestInterface $request)
{
$uri = sprintf('%s/shipments/%s/cancel', $this->endpoint, ...$request->getPathParams());
$this->logger->log(LogLevel::DEBUG, $uri);
try {
$this->auth->connect($this->accountId, $this->bearerToken);
$headers = $this->requestHeaders->getHeaders();
$rawResponse = $this->restClient->get($uri, [], $headers);
$this->logger->log(LogLevel::DEBUG, $rawResponse);
/** @var GetShipment $response */
$response = $this->responseParser->parse($rawResponse, GetShipment::class);
$shipment = $response->getData();
} catch (RestClientErrorException $e) {
$this->logger->log(LogLevel::ERROR, $e->getMessage());
/** @var Errors $response */
$response = $this->responseParser->parse($e->getMessage(), Errors::class);
throw AdapterException::errorResponse($response, $e);
} catch (\Exception $e) {
throw AdapterException::create($e);
}
return $shipment;
}
}