Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Endpoints/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getOrder(int $id): Order
try {
$response = $this->client->get('/orders/' . $id);

SendcloudRequestException::fromResponse($response);
SendcloudRequestException::fromResponse($response, 200);

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

Expand Down Expand Up @@ -156,7 +156,7 @@ public function getOrders(
// Send request

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

// Parse response

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

SendcloudRequestException::fromResponse($response);
SendcloudRequestException::fromResponse($response, 200);

$body = $response->getBody()->getContents();
$json = json_decode($body, true);
Expand Down Expand Up @@ -224,7 +224,7 @@ public function createOrder(
$body = json_encode($orders);
$response = $this->client->post('/orders', [], $body);

SendcloudRequestException::fromResponse($response);
SendcloudRequestException::fromResponse($response, 201);

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

SendcloudRequestException::fromResponse($response);
SendcloudRequestException::fromResponse($response, 204);
} catch (Throwable $throwable) {
SendcloudRequestException::fromException($throwable);
}
Expand Down
14 changes: 12 additions & 2 deletions src/Exceptions/SendcloudRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ public function __construct(
$message = $message ?? '';
$code = $code ?? SendcloudRequestException::CODE_UNKNOWN;

if (isset($sendcloudCode)) {
$message .= sprintf(' [%s]', $sendcloudCode);
}

if (isset($sendcloudSource)) {
$message .= " (at " . json_encode($sendcloudSource) . ")";
}

parent::__construct($message, $code, $previous);
}

/**
* Checks response for errors code and throws exception if needed
*
* @param $expected_status_code Expected HTTP status code in response
*
* @throws SendcloudRequestException
*/
public static function fromResponse(ResponseInterface $response) : void
public static function fromResponse(ResponseInterface $response, int $expected_status_code = 200) : void
{
if ($response->getStatusCode() === 200) {
if ($response->getStatusCode() === $expected_status_code) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Factory/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public static function create(

// Headers

$headers = [];
$headers = [
'Content-Type' => 'application/json'
];
if (isset($partnerId)) {
$headers['Sendcloud-Partner-Id'] = $partnerId;
}
Expand Down
17 changes: 10 additions & 7 deletions src/Models/Order/OrderDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function __construct(
public readonly Status $status,
public readonly DateTimeImmutable $order_created_at,
public readonly array $order_items,
public readonly DateTimeImmutable $order_updated_at,
public readonly string $notes,
public readonly ?DateTimeImmutable $order_updated_at = null,
public readonly ?string $notes = null,
public readonly ?array $tags = null
) {

Expand All @@ -50,8 +50,8 @@ public static function fromData(array $data) : self
status: Status::fromData($data['status']),
order_created_at: DateUtils::iso8601ToDateTime($data['order_created_at']),
order_items: $order_items,
order_updated_at: DateUtils::iso8601ToDateTime($data['order_updated_at']),
notes: (string) $data['notes'],
order_updated_at: isset($data['order_updated_at']) ? DateUtils::iso8601ToDateTime($data['order_updated_at']) : null,
notes: isset($data['notes']) ? (string) $data['notes'] : null,
tags: isset($data['tags']) ? $data['tags'] : null
);
}
Expand All @@ -62,12 +62,15 @@ public function jsonSerialize() : array
'integration' => $this->integration,
'status' => $this->status,
'order_created_at' => DateUtils::dateTimeToIso8601($this->order_created_at),
'order_items' => $this->order_items,
'order_updated_at' => DateUtils::dateTimeToIso8601($this->order_updated_at),
'notes' => $this->notes
'order_items' => $this->order_items
];

if (isset($this->order_updated_at)) {
$json['order_updated_at'] = DateUtils::dateTimeToIso8601($this->order_updated_at);
}

JsonUtils::addIfNotNull($json, 'tags', $this->tags);
JsonUtils::addIfNotNull($json, 'notes', $this->notes);

return $json;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Endpoints/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public function testCreateOrder() : void
]
}
JSON;
$endpoint = $this->getEndpoint($json, 200);
$endpoint = $this->getEndpoint($json, 201);

// -- Act

Expand Down Expand Up @@ -561,7 +561,7 @@ public function testDeleteOrder() : void
// -- Arrange

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

// -- Act & Assert

Expand Down