Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Dec 5, 2024
1 parent 82cc035 commit 34f8dd5
Show file tree
Hide file tree
Showing 108 changed files with 4,535 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/Http/Requests/GetAllMethodsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class GetAllMethodsRequest extends ResourceHydratableRequest

private GetAllMethodsQuery $query;

public function __construct(GetAllMethodsQuery $query)
public function __construct(?GetAllMethodsQuery $query = null)
{
$this->query = $query;
$this->query = $query ?: new GetAllMethodsQuery();
}

protected function defaultQuery(): array
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Requests/GetClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ class GetClientRequest extends ResourceHydratableRequest

private string $id;

private GetClientQuery $query;
private ?GetClientQuery $query;

public function __construct(string $id, GetClientQuery $query)
public function __construct(string $id, ?GetClientQuery $query = null)
{
$this->id = $id;
$this->query = $query;
}

protected function defaultQuery(): array
{
return $this->query->toArray();
return $this->query ? $this->query->toArray() : [];
}

public function resolveResourcePath(): string
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Requests/GetEnabledMethodsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class GetEnabledMethodsRequest extends ResourceHydratableRequest implements Supp

public static string $targetResourceClass = MethodCollection::class;

private GetEnabledPaymentMethodsQuery $query;
private ?GetEnabledPaymentMethodsQuery $query = null;

public function __construct(GetEnabledPaymentMethodsQuery $query)
public function __construct(?GetEnabledPaymentMethodsQuery $query = null)
{
$this->query = $query;
}

protected function defaultQuery(): array
{
return $this->query->toArray();
return $this->query ? $this->query->toArray() : [];
}

public function resolveResourcePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GetPaginatedBalanceTransactionRequest extends PaginatedRequest implements

public function __construct(
string $balanceId,
PaginatedQuery $query
?PaginatedQuery $query = null
) {
parent::__construct($query);

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/GetPaginatedCustomerPaymentsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GetPaginatedCustomerPaymentsRequest extends PaginatedRequest implements Is

public function __construct(
string $customerId,
GetPaginatedCustomerPaymentsQuery $query
?GetPaginatedCustomerPaymentsQuery $query = null
) {
parent::__construct($query);

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/GetPaginatedMandateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GetPaginatedMandateRequest extends PaginatedRequest implements IsIteratabl

private string $customerId;

public function __construct(string $customerId, PaginatedQuery $query)
public function __construct(string $customerId, ?PaginatedQuery $query = null)
{
parent::__construct($query);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GetPaginatedSubscriptionPaymentsRequest extends PaginatedRequest implement

private string $subscriptionId;

public function __construct(string $customerId, string $subscriptionId, PaginatedQuery $query)
public function __construct(string $customerId, string $subscriptionId, ?PaginatedQuery $query = null)
{
$this->customerId = $customerId;
$this->subscriptionId = $subscriptionId;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/GetPaginatedSubscriptionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GetPaginatedSubscriptionsRequest extends PaginatedRequest implements IsIte

private string $customerId;

public function __construct(string $customerId, PaginatedQuery $query)
public function __construct(string $customerId, ?PaginatedQuery $query = null)
{
$this->customerId = $customerId;

Expand Down
6 changes: 3 additions & 3 deletions src/Http/Requests/GetPaymentMethodRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ class GetPaymentMethodRequest extends ResourceHydratableRequest implements Suppo

public static string $targetResourceClass = Method::class;

private GetPaymentMethodQuery $query;
private ?GetPaymentMethodQuery $query = null;

private string $methodId;

public function __construct(string $methodId, GetPaymentMethodQuery $query)
public function __construct(string $methodId, ?GetPaymentMethodQuery $query = null)
{
$this->methodId = $methodId;
$this->query = $query;
}

protected function defaultQuery(): array
{
return $this->query->toArray();
return $this->query ? $this->query->toArray() : [];
}

public function resolveResourcePath(): string
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Requests/GetPaymentRefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class GetPaymentRefundRequest extends ResourceHydratableRequest implements Suppo

private string $refundId;

private GetPaymentRefundQuery $query;
private ?GetPaymentRefundQuery $query = null;

public function __construct(string $paymentId, string $refundId, GetPaymentRefundQuery $query)
public function __construct(string $paymentId, string $refundId, ?GetPaymentRefundQuery $query = null)
{
$this->paymentId = $paymentId;
$this->refundId = $refundId;
Expand All @@ -34,7 +34,7 @@ public function __construct(string $paymentId, string $refundId, GetPaymentRefun

protected function defaultQuery(): array
{
return $this->query->toArray();
return $this->query ? $this->query->toArray() : [];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Requests/GetPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class GetPaymentRequest extends ResourceHydratableRequest implements SupportsTes

private string $id;

private GetPaymentQuery $query;
private ?GetPaymentQuery $query = null;

public function __construct(
string $id,
GetPaymentQuery $query
?GetPaymentQuery $query = null
) {
$this->id = $id;
$this->query = $query;
}

protected function defaultQuery(): array
{
return $this->query->toArray();
return $this->query ? $this->query->toArray() : [];
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Mollie\Api\Contracts\Connector;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Http\Requests\ResourceHydratableRequest;
use Mollie\Api\Resources\BaseResource;
use Mollie\Api\Resources\ResourceCollection;
use Mollie\Api\Traits\DelegatesToResource;
use Mollie\Api\Traits\HandlesResourceHydration;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -14,6 +17,7 @@

class Response
{
use DelegatesToResource;
use HandlesResourceHydration;

protected ResponseInterface $psrResponse;
Expand All @@ -24,6 +28,11 @@ class Response

protected ?Throwable $senderException = null;

/**
* @var null|BaseResource|ResourceCollection
*/
protected $resource = null;

/**
* The decoded JSON response.
*/
Expand All @@ -42,15 +51,15 @@ public function __construct(
}

/**
* @return mixed
* @return self|BaseResource|ResourceCollection|null
*/
public function toResource()
{
if (! $this->getRequest() instanceof ResourceHydratableRequest) {
return $this;
}

return $this->hydrate($this->getRequest(), $this);
return $this->resource ?: $this->resource = $this->hydrate($this->getRequest(), $this);
}

/**
Expand Down
54 changes: 15 additions & 39 deletions src/Resources/CursorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,17 @@

use Generator;
use Mollie\Api\Http\Requests\DynamicGetRequest;
use Mollie\Api\Http\Requests\ResourceHydratableRequest;
use Mollie\Api\Http\Response;

abstract class CursorCollection extends ResourceCollection
{
private bool $autoHydrate = false;

public function setAutoHydrate(bool $shouldAutoHydrate = true): void
{
$this->autoHydrate = $shouldAutoHydrate;
}

public function shouldAutoHydrate(): bool
{
if ($this->response === null) {
return $this->autoHydrate;
}

$request = $this->response->getRequest();

/**
* Don't try to hydrate when the request
* already has auto-hydration enabled. The
* Hydrate Middleware will take care of that.
*/
if ($request instanceof ResourceHydratableRequest && $request->shouldAutoHydrate()) {
return false;
}

return $this->autoHydrate;
}

/**
* Return the next set of resources when available
*
* @return null|CursorCollection|Response
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function next(): ?CursorCollection
public function next()
{
if (! $this->hasNext()) {
return null;
Expand All @@ -52,9 +26,10 @@ public function next(): ?CursorCollection
/**
* Return the previous set of resources when available
*
* @return null|CursorCollection|Response
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function previous(): ?CursorCollection
public function previous()
{
if (! $this->hasPrevious()) {
return null;
Expand All @@ -63,15 +38,14 @@ public function previous(): ?CursorCollection
return $this->fetchCollection($this->_links->previous->href);
}

private function fetchCollection(string $url): CursorCollection
/**
* @return CursorCollection|Response
*/
private function fetchCollection(string $url)
{
$response = $this
return $this
->connector
->send(new DynamicGetRequest($url, static::class));

return $this->shouldAutoHydrate()
? $response->toResource()
: $response;
}

/**
Expand Down Expand Up @@ -99,8 +73,6 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection

return new LazyCollection(function () use ($page, $iterateBackwards): Generator {
while (true) {
$page->setAutoHydrate();

foreach ($page as $item) {
yield $item;
}
Expand All @@ -109,9 +81,13 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection
break;
}

$page = $iterateBackwards
$response = $iterateBackwards
? $page->previous()
: $page->next();

$page = $response instanceof Response
? $response->toResource()
: $response;
}
});
}
Expand Down
Loading

0 comments on commit 34f8dd5

Please sign in to comment.