Skip to content

Commit

Permalink
refactor #167 [Refund] Do not expose exception messages to the custom…
Browse files Browse the repository at this point in the history
…er (lchrusciel, Zales0123)

This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

1bc2a27 [Refund] Do not expose exception messages to the customer
f2bd489 Add missing dependency
  • Loading branch information
GSadee authored Dec 3, 2019
2 parents aa2c651 + f2bd489 commit 219d6ea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
28 changes: 25 additions & 3 deletions src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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');
}
}
8 changes: 4 additions & 4 deletions src/Creator/RefundUnitsCommandCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'),
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<argument type="service" id="session" />
<argument type="service" id="router" />
<argument type="service" id="Sylius\RefundPlugin\Creator\RefundUnitsCommandCreator" />
<argument type="service" id="monolog.logger" />
</service>

<service id="Sylius\RefundPlugin\Action\CompleteRefundPaymentAction">
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/flashes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 219d6ea

Please sign in to comment.