diff --git a/src/Action/Admin/RefundUnitsAction.php b/src/Action/Admin/RefundUnitsAction.php index 0aee8e9d..be133fc2 100644 --- a/src/Action/Admin/RefundUnitsAction.php +++ b/src/Action/Admin/RefundUnitsAction.php @@ -4,7 +4,9 @@ namespace Sylius\RefundPlugin\Action\Admin; +use Psr\Log\LoggerInterface; use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface; +use Sylius\RefundPlugin\Exception\InvalidRefundAmountException; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -27,16 +29,21 @@ final class RefundUnitsAction /** @var RefundUnitsCommandCreatorInterface */ private $commandCreator; + /** @var LoggerInterface */ + private $logger; + public function __construct( MessageBusInterface $commandBus, Session $session, UrlGeneratorInterface $router, - RefundUnitsCommandCreatorInterface $commandCreator + RefundUnitsCommandCreatorInterface $commandCreator, + LoggerInterface $logger ) { $this->commandBus = $commandBus; $this->session = $session; $this->router = $router; $this->commandCreator = $commandCreator; + $this->logger = $logger; } public function __invoke(Request $request): Response @@ -45,17 +52,32 @@ public function __invoke(Request $request): Response $this->commandBus->dispatch($this->commandCreator->fromRequest($request)); $this->session->getFlashBag()->add('success', 'sylius_refund.units_successfully_refunded'); - } catch (\InvalidArgumentException $exception) { + } catch (InvalidRefundAmountException $exception) { $this->session->getFlashBag()->add('error', $exception->getMessage()); + + $this->logger->error($exception->getMessage()); } catch (HandlerFailedException $exception) { /** @var \Exception $previousException */ $previousException = $exception->getPrevious(); - $this->session->getFlashBag()->add('error', $previousException->getMessage()); + $this->provideErrorMessage($previousException); + + $this->logger->error($previousException->getMessage()); } return new RedirectResponse($this->router->generate( 'sylius_refund_order_refunds_list', ['orderNumber' => $request->attributes->get('orderNumber')] )); } + + private function provideErrorMessage(\Exception $previousException): void + { + if ($previousException instanceof InvalidRefundAmountException) { + $this->session->getFlashBag()->add('error', $previousException->getMessage()); + + return; + } + + $this->session->getFlashBag()->add('error', 'sylius_refund.error_occurred'); + } } diff --git a/src/Creator/RefundUnitsCommandCreator.php b/src/Creator/RefundUnitsCommandCreator.php index 61779daf..0916a273 100644 --- a/src/Creator/RefundUnitsCommandCreator.php +++ b/src/Creator/RefundUnitsCommandCreator.php @@ -6,6 +6,7 @@ use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface; use Sylius\RefundPlugin\Command\RefundUnits; +use Sylius\RefundPlugin\Exception\InvalidRefundAmountException; use Sylius\RefundPlugin\Model\OrderItemUnitRefund; use Sylius\RefundPlugin\Model\RefundType; use Sylius\RefundPlugin\Model\ShipmentRefund; @@ -30,10 +31,9 @@ public function fromRequest(Request $request): RefundUnits $units = $this->filterEmptyRefundUnits($request->request->get('sylius_refund_units', [])); $shipments = $this->filterEmptyRefundUnits($request->request->get('sylius_refund_shipments', [])); - Assert::false( - count($units) === 0 && count($shipments) === 0, - 'sylius_refund.at_least_one_unit_should_be_selected_to_refund' - ); + if (count($units) === 0 && count($shipments) === 0) { + throw InvalidRefundAmountException::withValidationConstraint('sylius_refund.at_least_one_unit_should_be_selected_to_refund'); + } return new RefundUnits( $request->attributes->get('orderNumber'), diff --git a/src/Resources/config/services/actions.xml b/src/Resources/config/services/actions.xml index 540db118..922b04e1 100644 --- a/src/Resources/config/services/actions.xml +++ b/src/Resources/config/services/actions.xml @@ -29,6 +29,7 @@ + diff --git a/src/Resources/translations/flashes.en.yml b/src/Resources/translations/flashes.en.yml index 868fe157..97bfa9fb 100644 --- a/src/Resources/translations/flashes.en.yml +++ b/src/Resources/translations/flashes.en.yml @@ -8,3 +8,4 @@ sylius_refund: resend_credit_memo_success: 'Selected credit memo has been successfully resent' unit_refund_exceeded: 'You cannot refund more money than the order unit total' units_successfully_refunded: 'Selected order units have been successfully refunded' + error_occurred: 'Unexpected error occurred'