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
12 changes: 11 additions & 1 deletion src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

private ?MapperBuilder $mapperBuilder = null;

private bool $debug = false;

public function __construct(private readonly string $username, private readonly string $apiKey)
{
$this->logger = new NullLogger();
Expand All @@ -79,14 +81,14 @@
sprintf('Basic %s', base64_encode($this->username . ':' . $this->apiKey)),
)->withHeader('Accept', 'application/json');

if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {

Check warning on line 84 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ public function request(RequestInterface $request) : ResponseInterface { $request = $request->withHeader('Authorization', sprintf('Basic %s', base64_encode($this->username . ':' . $this->apiKey)))->withHeader('Accept', 'application/json'); - if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) { + if (!in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) { $request = $request->withHeader('Content-Type', 'application/json'); } $this->lastRequest = $request;

Check warning on line 84 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ public function request(RequestInterface $request) : ResponseInterface { $request = $request->withHeader('Authorization', sprintf('Basic %s', base64_encode($this->username . ':' . $this->apiKey)))->withHeader('Accept', 'application/json'); - if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) { + if (in_array($request->getMethod(), ['PUT', 'PATCH'])) { $request = $request->withHeader('Content-Type', 'application/json'); } $this->lastRequest = $request;
$request = $request->withHeader('Content-Type', 'application/json');
}

$this->lastRequest = $request;
$this->lastResponse = $this->getHttpClient()->sendRequest($this->lastRequest);

self::assertStatusCode($this->lastResponse);

Check warning on line 91 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } $this->lastRequest = $request; $this->lastResponse = $this->getHttpClient()->sendRequest($this->lastRequest); - self::assertStatusCode($this->lastResponse); + return $this->lastResponse; } public function get(string $uri, Query|array $query = []) : ResponseInterface

return $this->lastResponse;
}
Expand Down Expand Up @@ -135,7 +137,7 @@
{
if (null === $this->paymentGatewaysEndpoint) {
$this->paymentGatewaysEndpoint = new PaymentGatewaysEndpoint($this, $this->getMapperBuilder(), 'payment_gateways');
$this->paymentGatewaysEndpoint->setLogger($this->logger);

Check warning on line 140 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { if (null === $this->paymentGatewaysEndpoint) { $this->paymentGatewaysEndpoint = new PaymentGatewaysEndpoint($this, $this->getMapperBuilder(), 'payment_gateways'); - $this->paymentGatewaysEndpoint->setLogger($this->logger); + } return $this->paymentGatewaysEndpoint; }
}

return $this->paymentGatewaysEndpoint;
Expand Down Expand Up @@ -165,7 +167,7 @@
{
if (null === $this->shipmentTemplatesEndpoint) {
$this->shipmentTemplatesEndpoint = new ShipmentTemplatesEndpoint($this, $this->getMapperBuilder(), 'shipment_templates');
$this->shipmentTemplatesEndpoint->setLogger($this->logger);

Check warning on line 170 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { if (null === $this->shipmentTemplatesEndpoint) { $this->shipmentTemplatesEndpoint = new ShipmentTemplatesEndpoint($this, $this->getMapperBuilder(), 'shipment_templates'); - $this->shipmentTemplatesEndpoint->setLogger($this->logger); + } return $this->shipmentTemplatesEndpoint; }
}

return $this->shipmentTemplatesEndpoint;
Expand All @@ -175,7 +177,7 @@
{
if (null === $this->webhooksEndpoint) {
$this->webhooksEndpoint = new WebhooksEndpoint($this, $this->getMapperBuilder(), 'webhooks');
$this->webhooksEndpoint->setLogger($this->logger);

Check warning on line 180 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { if (null === $this->webhooksEndpoint) { $this->webhooksEndpoint = new WebhooksEndpoint($this, $this->getMapperBuilder(), 'webhooks'); - $this->webhooksEndpoint->setLogger($this->logger); + } return $this->webhooksEndpoint; }
}

return $this->webhooksEndpoint;
Expand Down Expand Up @@ -212,9 +214,17 @@
$this->logger = $logger;
}

public function setDebug(bool $debug): void
{
$this->debug = $debug;
}

private function getBaseUri(): string
{
return 'https://app.shipmondo.com/api/public/v3';
return sprintf(
'https://%s.shipmondo.com/api/public/v3',
$this->debug ? 'sandbox' : 'app',
);
}

private function getHttpClient(): HttpClientInterface
Expand Down Expand Up @@ -248,7 +258,7 @@
{
$statusCode = $response->getStatusCode();

if ($statusCode >= 200 && $statusCode < 300) {

Check warning on line 261 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ private static function assertStatusCode(ResponseInterface $response) : void { $statusCode = $response->getStatusCode(); - if ($statusCode >= 200 && $statusCode < 300) { + if ($statusCode >= 200 || $statusCode < 300) { return; } NotAuthorizedException::assert($response);

Check warning on line 261 in src/Client/Client.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ private static function assertStatusCode(ResponseInterface $response) : void { $statusCode = $response->getStatusCode(); - if ($statusCode >= 200 && $statusCode < 300) { + if ($statusCode >= 200 && $statusCode <= 300) { return; } NotAuthorizedException::assert($response);
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/Client/Endpoint/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

/**
* @template TResponse of Response
*
* @implements EndpointInterface<TResponse>
*/
abstract class Endpoint implements EndpointInterface, LoggerAwareInterface
Expand Down
18 changes: 18 additions & 0 deletions src/Client/Endpoint/ReadableEndpointInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Setono\Shipmondo\Client\Endpoint;

use Setono\Shipmondo\Response\Response;

/**
* @template TResponse of Response
*/
interface ReadableEndpointInterface
{
/**
* @return TResponse
*/
public function getById(int $id): Response;
}
31 changes: 31 additions & 0 deletions src/Client/Endpoint/ReadableEndpointTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Setono\Shipmondo\Client\Endpoint;

use Setono\Shipmondo\Response\Response;

/**
* @mixin Endpoint
*
* @template TResponse of Response
*/
trait ReadableEndpointTrait
{
/**
* @return TResponse
*/
public function getById(int $id): Response
{
return $this
->mapperBuilder
->mapper()
->map(
self::getResponseClass(),
$this->createSource(
$this->client->get(sprintf('%s/%d', $this->endpoint, $id)),
),
);
}
}
5 changes: 5 additions & 0 deletions src/Client/Endpoint/SalesOrdersEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class SalesOrdersEndpoint extends Endpoint implements SalesOrdersEndpointI
*/
use CreatableEndpointTrait;

/**
* @use ReadableEndpointTrait<SalesOrderResponse>
*/
use ReadableEndpointTrait;

protected static function getResponseClass(): string
{
return SalesOrderResponse::class;
Expand Down
3 changes: 2 additions & 1 deletion src/Client/Endpoint/SalesOrdersEndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
/**
* @extends EndpointInterface<SalesOrderResponse>
* @extends CreatableEndpointInterface<SalesOrderResponse>
* @extends ReadableEndpointInterface<SalesOrderResponse>
*/
interface SalesOrdersEndpointInterface extends EndpointInterface, CreatableEndpointInterface
interface SalesOrdersEndpointInterface extends EndpointInterface, CreatableEndpointInterface, ReadableEndpointInterface
{
}
1 change: 0 additions & 1 deletion src/Response/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* @template T
*
* @implements \IteratorAggregate<int, T>
*/
final class Collection implements \Countable, \IteratorAggregate
Expand Down
Loading