Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Sep 9, 2024
1 parent 164c1e0 commit 0115efa
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 110 deletions.
41 changes: 41 additions & 0 deletions src/EndpointCollection/OrganizationEndpointCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Mollie\Api\EndpointCollection;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Helpers;
use Mollie\Api\Http\Requests\GetOrganizationRequest;
use Mollie\Api\Resources\Organization;

class OrganizationEndpointCollection extends EndpointCollection
{
/**
* Retrieve an organization from Mollie.
*
* Will throw a ApiException if the organization id is invalid or the resource cannot be found.
*
* @param array|bool $testmode
*
* @throws ApiException
*/
public function get(string $id, $testmode = []): Organization
{
$testmode = Helpers::extractBool($testmode, 'testmode', false);

/** @var Organization */
return $this->send(new GetOrganizationRequest($id, $testmode));
}

/**
* Retrieve the current organization from Mollie.
*
* @param array|bool $testmode
*
* @throws ApiException
*/
public function current($testmode = []): Organization
{
/** @var Organization */
return $this->get('me', $testmode);
}
}
58 changes: 0 additions & 58 deletions src/Endpoints/OrganizationEndpoint.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Exceptions;

use DateTime;
use DateTimeImmutable;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
Expand Down Expand Up @@ -37,9 +37,9 @@ public function __construct(
) {
$this->plainMessage = $message;

$this->raisedAt = new \DateTimeImmutable;
$this->raisedAt = new DateTimeImmutable;

$formattedRaisedAt = $this->raisedAt->format(DateTime::ATOM);
$formattedRaisedAt = $this->raisedAt->format(DateTimeImmutable::ATOM);
$message = "[{$formattedRaisedAt}] ".$message;

if (! empty($field)) {
Expand Down Expand Up @@ -140,7 +140,7 @@ public function getRequest(): ?RequestInterface
/**
* Get the ISO8601 representation of the moment this exception was thrown
*/
public function getRaisedAt(): \DateTimeImmutable
public function getRaisedAt(): DateTimeImmutable
{
return $this->raisedAt;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Factories/GetBalanceReportQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Factories;

use DateTime;
use DateTimeImmutable;
use Mollie\Api\Http\Query\GetBalanceReportQuery;

class GetBalanceReportQueryFactory extends Factory
Expand All @@ -14,8 +14,8 @@ public function create(): GetBalanceReportQuery
}

return new GetBalanceReportQuery(
DateTime::createFromFormat('Y-m-d', $this->get('from')),
DateTime::createFromFormat('Y-m-d', $this->get('until')),
DateTimeImmutable::createFromFormat('Y-m-d', $this->get('from')),
DateTimeImmutable::createFromFormat('Y-m-d', $this->get('until')),
$this->get('grouping'),
$this->get('testmode')
);
Expand Down
4 changes: 2 additions & 2 deletions src/Factories/PaymentRouteCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Factories;

use DateTime;
use DateTimeImmutable;
use Mollie\Api\Helpers;
use Mollie\Api\Helpers\Arr;
use Mollie\Api\Http\Payload\DataCollection;
Expand All @@ -22,7 +22,7 @@ public function create(): DataCollection
Arr::get($item, 'destination.organizationId'),
Helpers::compose(
Arr::get($item, 'delayUntil'),
fn ($value) => DateTime::createFromFormat('Y-m-d', $value)
fn ($value) => DateTimeImmutable::createFromFormat('Y-m-d', $value)
)
);
}, $this->data);
Expand Down
4 changes: 2 additions & 2 deletions src/Factories/RecurringBillingCycleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Factories;

use DateTime;
use DateTimeImmutable;
use Mollie\Api\Http\Payload\RecurringBillingCycle;

class RecurringBillingCycleFactory extends Factory
Expand All @@ -14,7 +14,7 @@ public function create(): RecurringBillingCycle
$this->get('descriptipn'),
$this->mapIfNotNull('amount', fn (array $item) => MoneyFactory::new($item)->create()),
$this->get('times'),
$this->mapIfNotNull('startDate', fn (string $item) => DateTime::createFromFormat('Y-m-d', $item)),
$this->mapIfNotNull('startDate', fn (string $item) => DateTimeImmutable::createFromFormat('Y-m-d', $item)),
);
}
}
10 changes: 9 additions & 1 deletion src/Helpers/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ class Handler
{
private Closure $callback;

private ?string $name;

private string $priority;

public function __construct(Closure $callback, string $priority)
public function __construct(Closure $callback, ?string $name, string $priority)
{
$this->callback = $callback;
$this->name = $name;
$this->priority = $priority;
}

Expand All @@ -21,6 +24,11 @@ public function callback(): callable
return $this->callback;
}

public function name(): ?string
{
return $this->name;
}

public function priority(): string
{
return $this->priority;
Expand Down
22 changes: 20 additions & 2 deletions src/Helpers/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ class Handlers
*/
protected array $handlers = [];

public function add(callable $handler, string $priority): void
public function add(callable $handler, ?string $name = null, string $priority = MiddlewarePriority::MEDIUM): void
{
$this->handlers[] = new Handler($handler, $priority);
if (in_array($name, [MiddlewarePriority::HIGH, MiddlewarePriority::MEDIUM, MiddlewarePriority::LOW])) {
$priority = $name;
$name = null;
}

if (is_string($name) && $this->handlerExists($name)) {
throw new \InvalidArgumentException("Handler with name '{$name}' already exists.");
}

$this->handlers[] = new Handler($handler, $name, $priority);
}

public function setHandlers(array $handlers): void
Expand Down Expand Up @@ -70,4 +79,13 @@ protected function sortHandlers(): array

return array_merge($highPriority, $mediumPriority, $lowPriority);
}

private function handlerExists(string $name): bool
{
foreach ($this->handlers as $handler) {
if ($handler->name() === $name) {
return true;
}
}
}
}
42 changes: 24 additions & 18 deletions src/Helpers/MiddlewareHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct()
$this->onResponse = new Handlers;
}

public function onRequest(callable $callback, string $priority = MiddlewarePriority::MEDIUM): static
public function onRequest(callable $callback, ?string $name = null, string $priority = MiddlewarePriority::MEDIUM): static
{
$this->onRequest->add(static function (PendingRequest $pendingRequest) use ($callback): PendingRequest {
$result = $callback($pendingRequest);
Expand All @@ -28,12 +28,12 @@ public function onRequest(callable $callback, string $priority = MiddlewarePrior
}

return $pendingRequest;
}, $priority);
}, $name, $priority);

return $this;
}

public function onResponse(callable $callback, string $priority = MiddlewarePriority::MEDIUM): static
public function onResponse(callable $callback, ?string $name = null, string $priority = MiddlewarePriority::MEDIUM): static
{
$this->onResponse->add(static function (Response $response) use ($callback) {
$result = $callback($response);
Expand All @@ -42,7 +42,7 @@ public function onResponse(callable $callback, string $priority = MiddlewarePrio
|| $result instanceof ViableResponse
? $result
: $response;
}, $priority);
}, $name, $priority);

return $this;
}
Expand All @@ -60,21 +60,27 @@ public function executeOnResponse(Response $response)
return $this->onResponse->execute($response);
}

public function merge(MiddlewareHandlers $handlers): static
/**
* @param array<MiddlewareHandlers> ...$handlers
*/
public function merge(...$handlersCollection): static
{
$onRequestHandlers = array_merge(
$this->onRequest->getHandlers(),
$handlers->onRequest->getHandlers(),
);

$this->onRequest->setHandlers($onRequestHandlers);

$onResponseHandlers = array_merge(
$this->onResponse->getHandlers(),
$handlers->onResponse->getHandlers(),
);

$this->onResponse->setHandlers($onResponseHandlers);
/** @var MiddlewareHandlers $handlers */
foreach ($handlersCollection as $handlers) {
$onRequestHandlers = array_merge(
$this->onRequest->getHandlers(),
$handlers->onRequest->getHandlers()
);

$this->onRequest->setHandlers($onRequestHandlers);

$onResponseHandlers = array_merge(
$this->onResponse->getHandlers(),
$handlers->onResponse->getHandlers()
);

$this->onResponse->setHandlers($onResponseHandlers);
}

return $this;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Payload/PaymentRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Mollie\Api\Http\Payload;

use DateTime;
use DateTimeInterface;

class PaymentRoute extends DataBag
{
public function __construct(
public readonly Money $amount,
public readonly string $organizationId,
public readonly ?DateTime $delayUntil = null,
) {}
public readonly ?DateTimeInterface $delayUntil = null,
) {
}

public function data(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Payload/RecurringBillingCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Http\Payload;

use DateTime;
use DateTimeInterface;
use Mollie\Api\Rules\Matches;

class RecurringBillingCycle extends DataBag
Expand All @@ -20,14 +20,14 @@ class RecurringBillingCycle extends DataBag

public ?int $times;

public ?DateTime $startDate;
public ?DateTimeInterface $startDate;

public function __construct(
string $interval,
?string $description = null,
?Money $amount = null,
?int $times = null,
?DateTime $startDate = null,
?DateTimeInterface $startDate = null,
) {
$this->interval = $interval;
$this->description = $description;
Expand Down
10 changes: 5 additions & 5 deletions src/Http/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(Connector $connector, Request $request)
$this->method = $request->getMethod();
$this->url = Url::join($connector->resolveBaseUrl(), $request->resolveResourcePath());

$this->middleware()->merge($connector->middleware());
$this->middleware()->merge($request->middleware(), $connector->middleware());

$this
->tap(new MergeRequestProperties)
Expand All @@ -62,12 +62,12 @@ public function __construct(Connector $connector, Request $request)

$this
->middleware()
->onRequest(new EvaluateHydrationSetting)
->onRequest(new ApplyIdempotencyKey)
->onResponse(new ResetIdempotencyKey)
->onRequest(new EvaluateHydrationSetting, 'hydration')
->onRequest(new ApplyIdempotencyKey, 'idempotency')
->onResponse(new ResetIdempotencyKey, 'idempotency')
->onResponse(new GuardResponse, MiddlewarePriority::HIGH)
->onResponse(new ThrowExceptionIfRequestFailed, MiddlewarePriority::HIGH)
->onResponse(new Hydrate, MiddlewarePriority::LOW);
->onResponse(new Hydrate, 'hydration', MiddlewarePriority::LOW);

}

Expand Down
Loading

0 comments on commit 0115efa

Please sign in to comment.