Skip to content

Commit

Permalink
add type declarations to all endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 5, 2024
1 parent 6c9102e commit 1da742e
Show file tree
Hide file tree
Showing 40 changed files with 383 additions and 408 deletions.
2 changes: 1 addition & 1 deletion src/Endpoints/BalanceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mollie\Api\Resources\BaseCollection;
use Mollie\Api\Resources\LazyCollection;

class BalanceEndpoint extends CollectionEndpointAbstract
class BalanceEndpoint extends CollectionRestEndpoint
{
const string RESOURCE_ID_PREFIX = 'bal_';

Check failure on line 13 in src/Endpoints/BalanceEndpoint.php

View workflow job for this annotation

GitHub Actions / PHP - 8

Syntax error, unexpected T_STRING, expecting '=' on line 13

Check failure on line 13 in src/Endpoints/BalanceEndpoint.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Syntax error, unexpected T_STRING, expecting '=' on line 13

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/BalanceReportEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mollie\Api\Resources\BalanceReport;
use Mollie\Api\Resources\ResourceFactory;

class BalanceReportEndpoint extends EndpointAbstract
class BalanceReportEndpoint extends RestEndpoint
{
protected string $resourcePath = "balances_report";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/BalanceTransactionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Mollie\Api\Resources\BalanceTransactionCollection;
use Mollie\Api\Resources\LazyCollection;

class BalanceTransactionEndpoint extends CollectionEndpointAbstract
class BalanceTransactionEndpoint extends CollectionRestEndpoint
{
const string RESOURCE_ID_PREFIX = 'baltr_';

Check failure on line 14 in src/Endpoints/BalanceTransactionEndpoint.php

View workflow job for this annotation

GitHub Actions / PHP - 8

Syntax error, unexpected T_STRING, expecting '=' on line 14

Check failure on line 14 in src/Endpoints/BalanceTransactionEndpoint.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Syntax error, unexpected T_STRING, expecting '=' on line 14

Expand Down
91 changes: 91 additions & 0 deletions src/Endpoints/BaseEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Mollie\Api\Endpoints;

use Mollie\Api\Contracts\SingleResourceEndpoint;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;

abstract class BaseEndpoint
{
public const REST_CREATE = MollieApiClient::HTTP_POST;
public const REST_UPDATE = MollieApiClient::HTTP_PATCH;
public const REST_READ = MollieApiClient::HTTP_GET;
public const REST_LIST = MollieApiClient::HTTP_GET;
public const REST_DELETE = MollieApiClient::HTTP_DELETE;

protected MollieApiClient $client;

protected string $resourcePath;

protected ?string $parentId;

public function __construct(MollieApiClient $api)
{
$this->client = $api;
}

/**
* @param array $filters
* @return string
*/
protected function buildQueryString(array $filters): string
{
if (empty($filters)) {
return "";
}

foreach ($filters as $key => $value) {
if ($value === true) {
$filters[$key] = "true";
}

if ($value === false) {
$filters[$key] = "false";
}
}

return "?" . http_build_query($filters, "", "&");
}

/**
* @return string
* @throws ApiException
*/
public function getResourcePath(): string
{
if (strpos($this->resourcePath, "_") !== false) {
[$parentResource, $childResource] = explode("_", $this->resourcePath, 2);

if (empty($this->parentId)) {
throw new ApiException("Subresource '{$this->resourcePath}' used without parent '$parentResource' ID.");
}

return "$parentResource/{$this->parentId}/$childResource";
}

return $this->resourcePath;
}

protected function getPathToSingleResource(string $id): string
{
if ($this instanceof SingleResourceEndpoint) {
return $this->getResourcePath();
}

return "{$this->getResourcePath()}/{$id}";
}

/**
* @param array $body
* @return null|string
*/
protected function parseRequestBody(array $body): ?string
{
if (empty($body)) {
return null;
}

return @json_encode($body);
}
}
2 changes: 1 addition & 1 deletion src/Endpoints/ChargebackEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\ChargebackCollection;
use Mollie\Api\Resources\LazyCollection;

class ChargebackEndpoint extends CollectionEndpointAbstract
class ChargebackEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "chargebacks";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/ClientEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\ClientCollection;
use Mollie\Api\Resources\LazyCollection;

class ClientEndpoint extends CollectionEndpointAbstract
class ClientEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "clients";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/ClientLinkEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\ClientLink;

class ClientLinkEndpoint extends EndpointAbstract
class ClientLinkEndpoint extends RestEndpoint
{
protected string $resourcePath = "client-links";

Expand Down
39 changes: 26 additions & 13 deletions src/Endpoints/CollectionEndpointAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\ResourceFactory;

abstract class CollectionEndpointAbstract extends EndpointAbstract
abstract class CollectionRestEndpoint extends RestEndpoint
{
/**
* Get a collection of objects from the REST API.
Expand All @@ -22,20 +22,16 @@ abstract class CollectionEndpointAbstract extends EndpointAbstract
*/
protected function rest_list(?string $from = null, ?int $limit = null, array $filters = []): BaseCollection
{
$filters = array_merge(["from" => $from, "limit" => $limit], $filters);
$apiPath = $this->getResourcePath() . $this->buildQueryString(
$this->getMergedFilters($filters, $from, $limit)
);

$apiPath = $this->getResourcePath() . $this->buildQueryString($filters);
$result = $this->client->performHttpCall(
self::REST_LIST,
$apiPath
);

$result = $this->client->performHttpCall(self::REST_LIST, $apiPath);

/** @var BaseCollection $collection */
$collection = $this->getResourceCollectionObject($result->count, $result->_links);

foreach ($result->_embedded->{$collection->getCollectionResourceName()} as $dataResult) {
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
}

return $collection;
return $this->createCollectionFromResult($result);
}

/**
Expand All @@ -59,6 +55,23 @@ protected function rest_iterator(?string $from = null, ?int $limit = null, array
return $page->getAutoIterator($iterateBackwards);
}

protected function getMergedFilters(array $filters = [], ?string $from = null, ?int $limit = null): array
{
return array_merge(["from" => $from, "limit" => $limit], $filters);
}

protected function createCollectionFromResult(object $result): BaseCollection
{
/** @var BaseCollection $collection */
$collection = $this->getResourceCollectionObject($result->count, $result->_links);

foreach ($result->_embedded->{$collection->getCollectionResourceName()} as $dataResult) {
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
}

return $collection;
}

/**
* Get the collection object that is used by this API endpoint. Every API endpoint uses one type of collection object.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/CustomerEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\CustomerCollection;
use Mollie\Api\Resources\LazyCollection;

class CustomerEndpoint extends CollectionEndpointAbstract
class CustomerEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "customers";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/CustomerPaymentsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\PaymentCollection;

class CustomerPaymentsEndpoint extends CollectionEndpointAbstract
class CustomerPaymentsEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "customers_payments";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/InvoiceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\InvoiceCollection;
use Mollie\Api\Resources\LazyCollection;

class InvoiceEndpoint extends CollectionEndpointAbstract
class InvoiceEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "invoices";

Expand Down
6 changes: 3 additions & 3 deletions src/Endpoints/MandateEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\Mandate;
use Mollie\Api\Resources\MandateCollection;

class MandateEndpoint extends CollectionEndpointAbstract
class MandateEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "customers_mandates";

Expand Down Expand Up @@ -92,7 +92,7 @@ public function getForId(string $customerId, $mandateId, array $parameters = [])
* @return MandateCollection
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function listFor(Customer $customer, $from = null, $limit = null, array $parameters = []): MandateCollection
public function listFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = []): MandateCollection
{
return $this->listForId($customer->id, $from, $limit, $parameters);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public function iteratorFor(
* @return MandateCollection
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function listForId(string $customerId, string $from = null, int $limit = null, array $parameters = []): MandateCollection
public function listForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = []): MandateCollection
{
$this->parentId = $customerId;

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/MethodEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\MethodCollection;
use Mollie\Api\Resources\ResourceFactory;

class MethodEndpoint extends CollectionEndpointAbstract
class MethodEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "methods";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OnboardingEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Onboarding;

class OnboardingEndpoint extends EndpointAbstract implements SingleResourceEndpoint
class OnboardingEndpoint extends RestEndpoint implements SingleResourceEndpoint
{
protected string $resourcePath = "onboarding/me";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrderEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\OrderCollection;

class OrderEndpoint extends CollectionEndpointAbstract
class OrderEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "orders";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrderLineEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mollie\Api\Resources\OrderLineCollection;
use Mollie\Api\Resources\ResourceFactory;

class OrderLineEndpoint extends CollectionEndpointAbstract
class OrderLineEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "orders_lines";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrderPaymentEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\PaymentCollection;

class OrderPaymentEndpoint extends CollectionEndpointAbstract
class OrderPaymentEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "orders_payments";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrderRefundEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mollie\Api\Resources\Refund;
use Mollie\Api\Resources\RefundCollection;

class OrderRefundEndpoint extends CollectionEndpointAbstract
class OrderRefundEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "orders_refunds";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrganizationEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Organization;

class OrganizationEndpoint extends EndpointAbstract
class OrganizationEndpoint extends RestEndpoint
{
protected string $resourcePath = "organizations";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/OrganizationPartnerEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Partner;

class OrganizationPartnerEndpoint extends EndpointAbstract implements SingleResourceEndpoint
class OrganizationPartnerEndpoint extends RestEndpoint implements SingleResourceEndpoint
{
protected string $resourcePath = "organizations/me/partner";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/PaymentCaptureEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\Payment;

class PaymentCaptureEndpoint extends CollectionEndpointAbstract
class PaymentCaptureEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "payments_captures";

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/PaymentChargebackEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\Payment;

class PaymentChargebackEndpoint extends CollectionEndpointAbstract
class PaymentChargebackEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "payments_chargebacks";

Expand Down
16 changes: 3 additions & 13 deletions src/Endpoints/PaymentEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\PaymentCollection;
use Mollie\Api\Resources\Refund;
use Mollie\Api\Resources\ResourceFactory;

class PaymentEndpoint extends CollectionEndpointAbstract
class PaymentEndpoint extends CollectionRestEndpoint
{
protected string $resourcePath = "payments";

Expand Down Expand Up @@ -160,19 +159,10 @@ public function iterator(?string $from = null, ?int $limit = null, array $parame
*
* @return Refund
* @throws ApiException
* @todo: Refactor
*/
public function refund(Payment $payment, $data = []): Refund
{
$resource = "{$this->getResourcePath()}/" . urlencode($payment->id) . "/refunds";

$body = null;
if (($data === null ? 0 : count($data)) > 0) {
$body = json_encode($data);
}

$result = $this->client->performHttpCall(self::REST_CREATE, $resource, $body);

return ResourceFactory::createFromApiResult($result, new Refund($this->client));
return (new PaymentRefundEndpoint($this->client))
->createFor($payment, $data);
}
}
Loading

0 comments on commit 1da742e

Please sign in to comment.