Skip to content

Commit

Permalink
Tag services and document the whole process
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 26, 2021
1 parent 82f42a1 commit 7cf06b7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ as shown below:
Online refund logic should be implemented if you need it.
As the first try for the possible customization, you can check out `Sylius\RefundPlugin\Event\UnitsRefunded` event.
## Post-refunding process
After units are refunded, there are multiple other processes that should be triggered. By default, after units refund, there should be **CreditMemo** and
**RefundPayment** generated. As they're strictly coupled with each other, **RefundPayment** is always created after the **CreditMemo**. Moreover, if **RefundPayment**
fails, related **CreditMemo** should not be created as well.

`\Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManager` service facilitates the whole process. If you want to add one or more steps to it, you should create
a service implementing `\Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessStepInterface`, and register if with proper tag:

```xml
<service id="App\ProcessManager\CustomAfterRefundProcessManager">
<tag name="sylius_refund.units_refunded.process_step" priority="0" />
</service>
```

Tagged services would be executed according to their priority (descending).

## Security issues

If you think that you have found a security issue, please do not use the issue tracker and do not post it publicly.
Expand Down
2 changes: 1 addition & 1 deletion spec/ProcessManager/UnitsRefundedProcessManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function let(
UnitsRefundedProcessStepInterface $creditMemoProcessManager,
UnitsRefundedProcessStepInterface $refundPaymentProcessManager
): void {
$this->beConstructedWith($creditMemoProcessManager, $refundPaymentProcessManager);
$this->beConstructedWith([$creditMemoProcessManager, $refundPaymentProcessManager]);
}

function it_implements_units_refunded_process_manager_interface(): void
Expand Down
22 changes: 9 additions & 13 deletions src/ProcessManager/UnitsRefundedProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@

final class UnitsRefundedProcessManager implements UnitsRefundedProcessManagerInterface
{
/** @var UnitsRefundedProcessStepInterface */
private $creditMemoProcessManager;
/** @var iterable|UnitsRefundedProcessStepInterface[] */
private $steps;

/** @var UnitsRefundedProcessStepInterface */
private $refundPaymentProcessManager;

public function __construct(
UnitsRefundedProcessStepInterface $creditMemoProcessManager,
UnitsRefundedProcessStepInterface $refundPaymentProcessManager
) {
$this->creditMemoProcessManager = $creditMemoProcessManager;
$this->refundPaymentProcessManager = $refundPaymentProcessManager;
public function __construct(iterable $steps)
{
$this->steps = $steps;
}

public function __invoke(UnitsRefunded $event): void
{
$this->creditMemoProcessManager->next($event);
$this->refundPaymentProcessManager->next($event);
/** @var UnitsRefundedProcessStepInterface $step */
foreach ($this->steps as $step) {
$step->next($event);
}
}
}
23 changes: 12 additions & 11 deletions src/Resources/config/services/event_bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>

<service
id="Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManagerInterface"
class="Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManager"
>
<argument type="service" id="Sylius\RefundPlugin\ProcessManager\CreditMemoProcessManager" />
<argument type="service" id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>

<service id="Sylius\RefundPlugin\Listener\UnitRefundedEventListener">
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolver" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
Expand All @@ -28,8 +19,12 @@
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>

<service id="Sylius\RefundPlugin\ProcessManager\CreditMemoProcessManager">
<argument type="service" id="sylius.command_bus" />
<service
id="Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManagerInterface"
class="Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManager"
>
<argument type="tagged_iterator" tag="sylius_refund.units_refunded.process_step"/>
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>

<service id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager">
Expand All @@ -38,6 +33,12 @@
<argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactory" />
<argument type="service" id="doctrine.orm.default_entity_manager" />
<argument type="service" id="sylius.event_bus" />
<tag name="sylius_refund.units_refunded.process_step" priority="50" />
</service>

<service id="Sylius\RefundPlugin\ProcessManager\CreditMemoProcessManager">
<argument type="service" id="sylius.command_bus" />
<tag name="sylius_refund.units_refunded.process_step" priority="100" />
</service>
</services>
</container>
8 changes: 5 additions & 3 deletions tests/Behat/Services/Factory/FailedRefundPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

final class FailedRefundPaymentFactory implements RefundPaymentFactoryInterface
{
private const FAILED_FILE = __DIR__.'/refund-payment-failed.json';

/** @var RefundPaymentFactoryInterface */
private $baseRefundPaymentFactory;

Expand All @@ -24,8 +26,8 @@ public function createWithData(
string $state,
int $paymentMethodId
): RefundPaymentInterface {
if (file_exists(__DIR__.'/refund-payment-failed.json')) {
unlink(__DIR__.'/refund-payment-failed.json');
if (file_exists(self::FAILED_FILE)) {
unlink(self::FAILED_FILE);

throw new \Exception('Refund payment creation failed');
}
Expand All @@ -35,6 +37,6 @@ public function createWithData(

public function failRefundPaymentCreation(): void
{
touch(__DIR__.'/refund-payment-failed.json');
touch(self::FAILED_FILE);
}
}
8 changes: 5 additions & 3 deletions tests/Behat/Services/Generator/FailedCreditMemoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

final class FailedCreditMemoGenerator implements CreditMemoGeneratorInterface
{
private const FAILED_FILE = __DIR__.'/credit-memo-failed.json';

/** @var CreditMemoGeneratorInterface */
private $baseCreditMemoGenerator;

Expand All @@ -25,8 +27,8 @@ public function generate(
array $shipments,
string $comment
): CreditMemoInterface {
if (file_exists(__DIR__.'/credit-memo-failed.json')) {
unlink(__DIR__.'/credit-memo-failed.json');
if (file_exists(self::FAILED_FILE)) {
unlink(self::FAILED_FILE);

throw new \Exception('Credit memo generation failed');
}
Expand All @@ -36,6 +38,6 @@ public function generate(

public function failCreditMemoGeneration(): void
{
touch(__DIR__.'/credit-memo-failed.json');
touch(self::FAILED_FILE);
}
}

0 comments on commit 7cf06b7

Please sign in to comment.