diff --git a/src/EndpointCollection/OrganizationEndpointCollection.php b/src/EndpointCollection/OrganizationEndpointCollection.php new file mode 100644 index 00000000..d7e1988a --- /dev/null +++ b/src/EndpointCollection/OrganizationEndpointCollection.php @@ -0,0 +1,41 @@ +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); + } +} diff --git a/src/Endpoints/OrganizationEndpoint.php b/src/Endpoints/OrganizationEndpoint.php deleted file mode 100644 index ea826c88..00000000 --- a/src/Endpoints/OrganizationEndpoint.php +++ /dev/null @@ -1,58 +0,0 @@ -readResource($organizationId, $parameters); - } - - /** - * Retrieve the current organization from Mollie. - * - * @param array $parameters - * - * @return Organization - * @throws ApiException - */ - public function current(array $parameters = []): Organization - { - /** @var Organization */ - return $this->readResource('me', $parameters); - } -} diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index b4e8b0ec..6b4bbdb4 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Exceptions; -use DateTime; +use DateTimeImmutable; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Throwable; @@ -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)) { @@ -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; } diff --git a/src/Factories/GetBalanceReportQueryFactory.php b/src/Factories/GetBalanceReportQueryFactory.php index a5c1c0a4..387114e1 100644 --- a/src/Factories/GetBalanceReportQueryFactory.php +++ b/src/Factories/GetBalanceReportQueryFactory.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Factories; -use DateTime; +use DateTimeImmutable; use Mollie\Api\Http\Query\GetBalanceReportQuery; class GetBalanceReportQueryFactory extends Factory @@ -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') ); diff --git a/src/Factories/PaymentRouteCollectionFactory.php b/src/Factories/PaymentRouteCollectionFactory.php index 10bcb665..678714c8 100644 --- a/src/Factories/PaymentRouteCollectionFactory.php +++ b/src/Factories/PaymentRouteCollectionFactory.php @@ -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; @@ -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); diff --git a/src/Factories/RecurringBillingCycleFactory.php b/src/Factories/RecurringBillingCycleFactory.php index 88cb22bb..0c0c9c20 100644 --- a/src/Factories/RecurringBillingCycleFactory.php +++ b/src/Factories/RecurringBillingCycleFactory.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Factories; -use DateTime; +use DateTimeImmutable; use Mollie\Api\Http\Payload\RecurringBillingCycle; class RecurringBillingCycleFactory extends Factory @@ -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)), ); } } diff --git a/src/Helpers/Handler.php b/src/Helpers/Handler.php index 22ac935b..b5944a64 100644 --- a/src/Helpers/Handler.php +++ b/src/Helpers/Handler.php @@ -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; } @@ -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; diff --git a/src/Helpers/Handlers.php b/src/Helpers/Handlers.php index 3b40bf7e..c6b4cf5c 100644 --- a/src/Helpers/Handlers.php +++ b/src/Helpers/Handlers.php @@ -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 @@ -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; + } + } + } } diff --git a/src/Helpers/MiddlewareHandlers.php b/src/Helpers/MiddlewareHandlers.php index 23bf595b..7a7ad5b9 100644 --- a/src/Helpers/MiddlewareHandlers.php +++ b/src/Helpers/MiddlewareHandlers.php @@ -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); @@ -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); @@ -42,7 +42,7 @@ public function onResponse(callable $callback, string $priority = MiddlewarePrio || $result instanceof ViableResponse ? $result : $response; - }, $priority); + }, $name, $priority); return $this; } @@ -60,21 +60,27 @@ public function executeOnResponse(Response $response) return $this->onResponse->execute($response); } - public function merge(MiddlewareHandlers $handlers): static + /** + * @param array ...$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; } diff --git a/src/Http/Payload/PaymentRoute.php b/src/Http/Payload/PaymentRoute.php index c8ce012c..0bad63da 100644 --- a/src/Http/Payload/PaymentRoute.php +++ b/src/Http/Payload/PaymentRoute.php @@ -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 { diff --git a/src/Http/Payload/RecurringBillingCycle.php b/src/Http/Payload/RecurringBillingCycle.php index 1aa35947..bf19ea4b 100644 --- a/src/Http/Payload/RecurringBillingCycle.php +++ b/src/Http/Payload/RecurringBillingCycle.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Http\Payload; -use DateTime; +use DateTimeInterface; use Mollie\Api\Rules\Matches; class RecurringBillingCycle extends DataBag @@ -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; diff --git a/src/Http/PendingRequest.php b/src/Http/PendingRequest.php index a177132e..c41c8f3d 100644 --- a/src/Http/PendingRequest.php +++ b/src/Http/PendingRequest.php @@ -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) @@ -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); } diff --git a/src/Http/Query/GetBalanceReportQuery.php b/src/Http/Query/GetBalanceReportQuery.php index 9ac9cb07..36f58928 100644 --- a/src/Http/Query/GetBalanceReportQuery.php +++ b/src/Http/Query/GetBalanceReportQuery.php @@ -2,21 +2,21 @@ namespace Mollie\Api\Http\Query; -use DateTime; +use DateTimeInterface; class GetBalanceReportQuery extends Query { - public DateTime $from; + public DateTimeInterface $from; - public DateTime $until; + public DateTimeInterface $until; public ?string $grouping; public ?bool $testmode; public function __construct( - DateTime $from, - DateTime $until, + DateTimeInterface $from, + DateTimeInterface $until, ?string $grouping = null, ?bool $testmode = null ) { diff --git a/src/Http/Request.php b/src/Http/Request.php index 40864661..0ed2015a 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -4,11 +4,13 @@ use LogicException; use Mollie\Api\Contracts\ValidatableDataProvider; +use Mollie\Api\Traits\HasMiddleware; use Mollie\Api\Traits\HasRequestProperties; use Mollie\Api\Traits\HasRules; abstract class Request implements ValidatableDataProvider { + use HasMiddleware; use HasRequestProperties; use HasRules; diff --git a/src/Http/Requests/GetOrganizationRequest.php b/src/Http/Requests/GetOrganizationRequest.php new file mode 100644 index 00000000..b47c5a2b --- /dev/null +++ b/src/Http/Requests/GetOrganizationRequest.php @@ -0,0 +1,18 @@ + OrderRefundEndpoint::class, 'orders' => OrderEndpointCollection::class, 'organizationPartners' => OrganizationPartnerEndpoint::class, - 'organizations' => OrganizationEndpoint::class, + 'organizations' => OrganizationEndpointCollection::class, 'payments' => PaymentEndpointCollection::class, 'paymentRefunds' => PaymentRefundEndpointCollection::class, 'paymentCaptures' => PaymentCaptureEndpoint::class,