Skip to content

Commit

Permalink
Dispatch event so that it's easier to hook into the availability service
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGallinari committed Sep 26, 2024
1 parent e55b4e7 commit 6160cfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion config/services/checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe_sylius_pausepay.checker.pause_pay_availability', PausePayAvailabilityChecker::class);
$services->set('webgriffe_sylius_pausepay.checker.pause_pay_availability', PausePayAvailabilityChecker::class)
->args([
service('event_dispatcher'),
]);
};
24 changes: 23 additions & 1 deletion src/Checker/PausePayAvailabilityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentInterface as BasePaymentInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusPausePayPlugin\Payum\PausePayApi;
use Webmozart\Assert\Assert;

Expand All @@ -18,6 +20,12 @@ final class PausePayAvailabilityChecker implements PaymentMethodAvailabilityChec

public const MAXIMUM_ORDER_AMOUNT = 2000000;

public const AVAILABILITY_EVENT_NAME = 'webgriffe.sylius_pausepay_plugin.checker.pausepay_availability';

public function __construct(private EventDispatcherInterface $eventDispatcher)
{
}

public function isAvailable(BasePaymentInterface $subject, PaymentMethodInterface $paymentMethod): bool
{
$gatewayConfig = $paymentMethod->getGatewayConfig();
Expand Down Expand Up @@ -48,6 +56,20 @@ public function isAvailable(BasePaymentInterface $subject, PaymentMethodInterfac

$orderAmount = $order->getTotal();

return $orderAmount >= self::MINIMUM_ORDER_AMOUNT && $orderAmount <= self::MAXIMUM_ORDER_AMOUNT;
$isAvailable = $orderAmount >= self::MINIMUM_ORDER_AMOUNT && $orderAmount <= self::MAXIMUM_ORDER_AMOUNT;

/** @var GenericEvent $event */
$event = $this->eventDispatcher->dispatch(
new GenericEvent($order, ['paymentMethod' => $paymentMethod, 'isAvailable' => $isAvailable]),
self::AVAILABILITY_EVENT_NAME,
);

/** @var mixed $result */
$result = $event->getArgument('isAvailable');
if (is_bool($result)) {
return $result;
}

return $isAvailable;
}
}

0 comments on commit 6160cfd

Please sign in to comment.