Skip to content

Commit 45efcce

Browse files
authored
Fixed quirks with Orders endpoint (#8)
* Added Content-Type * Better exception message * Added expected_status_code in response checker * feat(models): make order updated at and notes nullable in OrderDetails * Fixed type error * Fixed tests * Apply rector
1 parent 323b8cf commit 45efcce

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

src/Endpoints/Orders.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getOrder(int $id): Order
2525
try {
2626
$response = $this->client->get('/orders/' . $id);
2727

28-
SendcloudRequestException::fromResponse($response);
28+
SendcloudRequestException::fromResponse($response, 200);
2929

3030
$body = $response->getBody()->getContents();
3131

@@ -156,7 +156,7 @@ public function getOrders(
156156
// Send request
157157

158158
$response = $this->client->get($uri);
159-
SendcloudRequestException::fromResponse($response);
159+
SendcloudRequestException::fromResponse($response, 200);
160160

161161
// Parse response
162162

@@ -195,7 +195,7 @@ public function updateOrder(
195195
$body = json_encode($order);
196196
$response = $this->client->patch('/orders/' . $order->id, [], $body);
197197

198-
SendcloudRequestException::fromResponse($response);
198+
SendcloudRequestException::fromResponse($response, 200);
199199

200200
$body = $response->getBody()->getContents();
201201
$json = json_decode($body, true);
@@ -224,7 +224,7 @@ public function createOrder(
224224
$body = json_encode($orders);
225225
$response = $this->client->post('/orders', [], $body);
226226

227-
SendcloudRequestException::fromResponse($response);
227+
SendcloudRequestException::fromResponse($response, 201);
228228

229229
$body = $response->getBody()->getContents();
230230
$json = json_decode($body, true);
@@ -252,7 +252,7 @@ public function deleteOrder(
252252
try {
253253
$response = $this->client->post('/orders/' . $id);
254254

255-
SendcloudRequestException::fromResponse($response);
255+
SendcloudRequestException::fromResponse($response, 204);
256256
} catch (Throwable $throwable) {
257257
SendcloudRequestException::fromException($throwable);
258258
}

src/Exceptions/SendcloudRequestException.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,27 @@ public function __construct(
3333
$message = $message ?? '';
3434
$code = $code ?? SendcloudRequestException::CODE_UNKNOWN;
3535

36+
if (isset($sendcloudCode)) {
37+
$message .= sprintf(' [%s]', $sendcloudCode);
38+
}
39+
40+
if (isset($sendcloudSource)) {
41+
$message .= " (at " . json_encode($sendcloudSource) . ")";
42+
}
43+
3644
parent::__construct($message, $code, $previous);
3745
}
3846

3947
/**
4048
* Checks response for errors code and throws exception if needed
49+
*
50+
* @param $expected_status_code Expected HTTP status code in response
4151
*
4252
* @throws SendcloudRequestException
4353
*/
44-
public static function fromResponse(ResponseInterface $response) : void
54+
public static function fromResponse(ResponseInterface $response, int $expected_status_code = 200) : void
4555
{
46-
if ($response->getStatusCode() === 200) {
56+
if ($response->getStatusCode() === $expected_status_code) {
4757
return;
4858
}
4959

src/Factory/ClientFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public static function create(
5151

5252
// Headers
5353

54-
$headers = [];
54+
$headers = [
55+
'Content-Type' => 'application/json'
56+
];
5557
if (isset($partnerId)) {
5658
$headers['Sendcloud-Partner-Id'] = $partnerId;
5759
}

src/Models/Order/OrderDetails.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function __construct(
3131
public readonly Status $status,
3232
public readonly DateTimeImmutable $order_created_at,
3333
public readonly array $order_items,
34-
public readonly DateTimeImmutable $order_updated_at,
35-
public readonly string $notes,
34+
public readonly ?DateTimeImmutable $order_updated_at = null,
35+
public readonly ?string $notes = null,
3636
public readonly ?array $tags = null
3737
) {
3838

@@ -50,8 +50,8 @@ public static function fromData(array $data) : self
5050
status: Status::fromData($data['status']),
5151
order_created_at: DateUtils::iso8601ToDateTime($data['order_created_at']),
5252
order_items: $order_items,
53-
order_updated_at: DateUtils::iso8601ToDateTime($data['order_updated_at']),
54-
notes: (string) $data['notes'],
53+
order_updated_at: isset($data['order_updated_at']) ? DateUtils::iso8601ToDateTime($data['order_updated_at']) : null,
54+
notes: isset($data['notes']) ? (string) $data['notes'] : null,
5555
tags: isset($data['tags']) ? $data['tags'] : null
5656
);
5757
}
@@ -62,12 +62,15 @@ public function jsonSerialize() : array
6262
'integration' => $this->integration,
6363
'status' => $this->status,
6464
'order_created_at' => DateUtils::dateTimeToIso8601($this->order_created_at),
65-
'order_items' => $this->order_items,
66-
'order_updated_at' => DateUtils::dateTimeToIso8601($this->order_updated_at),
67-
'notes' => $this->notes
65+
'order_items' => $this->order_items
6866
];
6967

68+
if (isset($this->order_updated_at)) {
69+
$json['order_updated_at'] = DateUtils::dateTimeToIso8601($this->order_updated_at);
70+
}
71+
7072
JsonUtils::addIfNotNull($json, 'tags', $this->tags);
73+
JsonUtils::addIfNotNull($json, 'notes', $this->notes);
7174

7275
return $json;
7376
}

tests/Endpoints/OrdersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public function testCreateOrder() : void
525525
]
526526
}
527527
JSON;
528-
$endpoint = $this->getEndpoint($json, 200);
528+
$endpoint = $this->getEndpoint($json, 201);
529529

530530
// -- Act
531531

@@ -561,7 +561,7 @@ public function testDeleteOrder() : void
561561
// -- Arrange
562562

563563
$order_id = 1;
564-
$endpoint = $this->getEndpoint('', 200);
564+
$endpoint = $this->getEndpoint('', 204);
565565

566566
// -- Act & Assert
567567

0 commit comments

Comments
 (0)