Skip to content

Commit

Permalink
Turn decorators into middleware chain
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jan 3, 2025
1 parent 0fd207a commit e6a8f52
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 155 deletions.
11 changes: 5 additions & 6 deletions src/Payment/MollieSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mollie\WooCommerce\Payment;

use Mollie\Api\Types\SequenceType;
use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\Subscription\MollieSubscriptionGatewayHandler;

Expand All @@ -13,20 +14,21 @@ class MollieSubscription extends MollieObject
* @var mixed
*/
private $paymentMethod;
protected MiddlewareHandler $middleware;

/**
* Molliesubscription constructor.
*
*/
public function __construct($pluginId, Api $apiHelper, $settingsHelper, $dataHelper, $logger, $paymentMethod, $middleware)
public function __construct($pluginId, Api $apiHelper, $settingsHelper, $dataHelper, $logger, $paymentMethod, $middlewareHandler)
{
$this->pluginId = $pluginId;
$this->apiHelper = $apiHelper;
$this->settingsHelper = $settingsHelper;
$this->dataHelper = $dataHelper;
$this->logger = $logger;
$this->paymentMethod = $paymentMethod;
$this->middleware = $middleware;
$this->middleware = $middlewareHandler;
}
/**
* @param $order
Expand Down Expand Up @@ -67,10 +69,7 @@ public function getRecurringPaymentRequestData($order, $customerId, $initialPaym
'customerId' => $customerId,
]);
$context = 'payment';
foreach ($this->middleware as $field) {
$requestData = $field->decorate($requestData, $order, $context);
}
return $requestData;
return $this->middleware->handle($requestData, $order, $context);
}

protected function getRecurringPaymentDescription($order, $option, $initialPaymentUsedOrderAPI)
Expand Down
10 changes: 0 additions & 10 deletions src/Payment/Request/Decorators/RequestDecoratorInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use Psr\Container\ContainerInterface;
use WC_Order;

class AddCustomRequestFieldsDecorator implements RequestDecoratorInterface
class AddCustomRequestFieldsMiddleware implements RequestMiddlewareInterface
{
private array $paymentMethods;
private ContainerInterface $container;
Expand All @@ -18,23 +18,23 @@ public function __construct($paymentMethods, $container)
$this->container = $container;
}

public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
$methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1);
$paymentMethod = $this->paymentMethods[$methodId];
if (property_exists($paymentMethod, 'paymentAPIfields')) {
$paymentAPIfields = $paymentMethod->paymentAPIfields;
foreach ($paymentAPIfields as $field) {
$decoratorClass = 'Mollie\\WooCommerce\\Payment\\Decorator\\' . $field;
if (class_exists($decoratorClass)) {
$decorator = $this->container->get($decoratorClass);
if ($decorator instanceof RequestDecoratorInterface) {
$requestData = $decorator->decorate($requestData, $order);
$middlewareClass = 'Mollie\\WooCommerce\\Payment\\Request\\Middleware' . $field;
if (class_exists($middlewareClass)) {
$middleware = $this->container->get($middlewareClass);
if ($middleware instanceof RequestMiddlewareInterface) {
$requestData = $middleware->__invoke($requestData, $order);
}
}
}
}
return $requestData;
return $next($requestData, $order, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use Mollie\WooCommerce\Shared\Data;
use WC_Order;

class AddSequenceTypeForSubscriptionsDecorator implements RequestDecoratorInterface
class AddSequenceTypeForSubscriptionsMiddleware implements RequestMiddlewareInterface
{
private Data $dataHelper;
private string $pluginId;
Expand All @@ -18,13 +18,13 @@ public function __construct($dataHelper, $pluginId)
$this->pluginId = $pluginId;
}

public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if ($gateway) {
$requestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $requestData, $context);
}
return $requestData;
return $next($requestData, $order, $context);
}

private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $requestData, $context): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use stdClass;
use WC_Order;

class AddressDecorator implements RequestDecoratorInterface
class AddressMiddleware implements RequestMiddlewareInterface
{
public const MAXIMAL_LENGTH_ADDRESS = 100;
public const MAXIMAL_LENGTH_POSTALCODE = 20;
public const MAXIMAL_LENGTH_CITY = 200;
public const MAXIMAL_LENGTH_REGION = 200;
public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array
{
$isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton';
$billingAddress = null;
Expand All @@ -31,8 +31,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null):
) {
$requestData['shippingAddress'] = $shippingAddress;
}

return $requestData;
return $next($requestData, $order, $context);
}

private function createBillingAddress(WC_Order $order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use WC_Order;

class ApplePayTokenDecorator implements RequestDecoratorInterface
class ApplePayTokenMiddleware implements RequestMiddlewareInterface
{
public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
{
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$applePayToken = wc_clean(wp_unslash($_POST["token"] ?? ''));
if (!$applePayToken) {
return $requestData;
return $next($requestData, $order, $context);
}
$encodedApplePayToken = wp_json_encode($applePayToken);
if ($context === 'order') {
$requestData['payment']['applePayToken'] = $encodedApplePayToken;
} elseif ($context === 'payment') {
$requestData['applePayToken'] = $encodedApplePayToken;
}
return $requestData;
return $next($requestData, $order, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use WC_Order;

class CardTokenDecorator implements RequestDecoratorInterface
class CardTokenMiddleware implements RequestMiddlewareInterface
{
public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
{
$cardToken = mollieWooCommerceCardToken();
if ($cardToken && isset($requestData['payment']) && $context === 'order') {
$requestData['payment']['cardToken'] = $cardToken;
} elseif ($cardToken && isset($requestData['payment']) && $context === 'payment') {
$requestData['cardToken'] = $cardToken;
}
return $requestData;
return $next($requestData, $order, $context);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class CustomerBirthdateDecorator implements RequestDecoratorInterface
class CustomerBirthdateMiddleware implements RequestMiddlewareInterface
{
private array $paymentMethods;

Expand All @@ -14,7 +13,7 @@ public function __construct(array $paymentMethods)
$this->paymentMethods = $paymentMethods;
}

public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if (!$gateway || !isset($gateway->id)) {
Expand All @@ -40,6 +39,6 @@ public function decorate(array $requestData, WC_Order $order, $context = null):
$format = "Y-m-d";
$requestData['consumerDateOfBirth'] = gmdate($format, (int) strtotime($fieldPosted));
}
return $requestData;
return $next($requestData, $order, $context);
}
}
37 changes: 37 additions & 0 deletions src/Payment/Request/Middleware/MiddlewareHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Mollie\WooCommerce\Payment\Request\Middleware;

use WC_Order;

class MiddlewareHandler
{
private array $middlewares;

public function __construct(array $middlewares)
{
$this->middlewares = $middlewares;
}

public function handle(array $requestData, WC_Order $order, $context = null): array
{
$middlewareChain = $this->createMiddlewareChain($this->middlewares);

return $middlewareChain($requestData, $order, $context);
}

private function createMiddlewareChain(array $middlewares): callable
{
return array_reduce(
array_reverse($middlewares),
function ($next, $middleware) {
return function ($requestData, $order, $context) use ($middleware, $next) {
return $middleware($requestData, $order, $context, $next);
};
},
function ($requestData) {
return $requestData;
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use Mollie\WooCommerce\Payment\OrderLines;
use WC_Order;

class OrderLinesDecorator implements RequestDecoratorInterface
class OrderLinesMiddleware implements RequestMiddlewareInterface
{
private OrderLines $orderLines;
private string $voucherDefaultCategory;
Expand All @@ -18,10 +18,10 @@ public function __construct($orderLines, $voucherDefaultCategory)
$this->voucherDefaultCategory = $voucherDefaultCategory;
}

public function decorate(array $requestData, WC_Order $order, $context = null): array
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
{
$orderLines = $this->orderLines->order_lines($order, $this->voucherDefaultCategory);
$requestData['lines'] = $orderLines['lines'];
return $requestData;
return $next($requestData, $order, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Request\Decorators;
namespace Mollie\WooCommerce\Payment\Request\Middleware;

use WC_Order;

class PaymentDescriptionDecorator implements RequestDecoratorInterface
class PaymentDescriptionMiddleware implements RequestMiddlewareInterface
{
private $dataHelper;

Expand All @@ -15,14 +15,14 @@ public function __construct($dataHelper)
$this->dataHelper = $dataHelper;
}

public function decorate(array $requestData, WC_Order $order, string $context = null): array
public function __invoke(array $requestData, WC_Order $order, string $context = null, $next): array
{
$optionName = $this->dataHelper->getPluginId() . '_' . 'api_payment_description';
$option = get_option($optionName);
$paymentDescription = $this->getPaymentDescription($order, $option);

$requestData['description'] = $paymentDescription;
return $requestData;
return $next($requestData, $order, $context);
}

private function getPaymentDescription(WC_Order $order, $option): string
Expand Down
10 changes: 10 additions & 0 deletions src/Payment/Request/Middleware/RequestMiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Mollie\WooCommerce\Payment\Request\Middleware;

use WC_Order;

interface RequestMiddlewareInterface
{
public function __invoke(array $requestData, WC_Order $order, string $context = null, callable $next): array;
}
Loading

0 comments on commit e6a8f52

Please sign in to comment.