Skip to content

Commit

Permalink
[RefundPayment] Refactor factory to make it easier to overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jun 24, 2021
1 parent dcf22f3 commit 7e21428
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
18 changes: 18 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@
1. `sylius_refund_plugin.block_event_listener.account.order_show` and `sylius_refund_plugin.block_event_listener.order_show.credit_memos`
listeners have been replaced by `sylius_ui` configuration

1. `Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface` and `Sylius\RefundPlugin\Factory\RefundPaymentFactory`
have been removed and replaced by default resource factory `sylius_refund.factory.refund_payment`

1. The definition of `Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager` has been changed:

```diff
<service id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager">
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface" />
<argument type="service" id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface" />
- <argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface" />
+ <argument type="service" id="sylius_refund.factory.refund_payment" />
<argument type="service" id="sylius.repository.order" />
+ <argument type="service" id="sylius.repository.payment_method" />
<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>

### UPGRADE FROM 1.0.0-RC.9 TO 1.0.0-RC.10

1. Support for Sylius 1.8 has been dropped, upgrade your application to [Sylius 1.9](https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.9.md)
Expand Down
17 changes: 6 additions & 11 deletions src/Factory/RefundPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,25 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

final class RefundPaymentFactory implements RefundPaymentFactoryInterface
{
/** @var PaymentMethodRepositoryInterface */
private $paymentMethodRepository;
/** @var string */
private $className;

public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository)
public function __construct(string $className)
{
$this->paymentMethodRepository = $paymentMethodRepository;
$this->className = $className;
}

public function createWithData(
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
int $paymentMethodId
PaymentMethodInterface $paymentMethod
): RefundPaymentInterface {
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->find($paymentMethodId);

return new RefundPayment($order, $amount, $currencyCode, $state, $paymentMethod);
return new $this->className($order, $amount, $currencyCode, $state, $paymentMethod);
}
}
3 changes: 2 additions & 1 deletion src/Factory/RefundPaymentFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

interface RefundPaymentFactoryInterface
Expand All @@ -23,6 +24,6 @@ public function createWithData(
int $amount,
string $currencyCode,
string $state,
int $paymentMethodId
PaymentMethodInterface $paymentMethod
): RefundPaymentInterface;
}
13 changes: 12 additions & 1 deletion src/ProcessManager/RefundPaymentProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
Expand All @@ -39,6 +41,9 @@ final class RefundPaymentProcessManager implements UnitsRefundedProcessStepInter
/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var PaymentMethodRepositoryInterface */
private $paymentMethodRepository;

/** @var EntityManagerInterface */
private $entityManager;

Expand All @@ -50,13 +55,15 @@ public function __construct(
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
PaymentMethodRepositoryInterface $paymentMethodRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus
) {
$this->orderFullyRefundedStateResolver = $orderFullyRefundedStateResolver;
$this->relatedPaymentIdProvider = $relatedPaymentIdProvider;
$this->refundPaymentFactory = $refundPaymentFactory;
$this->orderRepository = $orderRepository;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->entityManager = $entityManager;
$this->eventBus = $eventBus;
}
Expand All @@ -67,12 +74,16 @@ public function next(UnitsRefunded $unitsRefunded): void
$order = $this->orderRepository->findOneByNumber($unitsRefunded->orderNumber());
Assert::notNull($order);

/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->find($unitsRefunded->paymentMethodId());
Assert::notNull($paymentMethod);

$refundPayment = $this->refundPaymentFactory->createWithData(
$order,
$unitsRefunded->amount(),
$unitsRefunded->currencyCode(),
RefundPaymentInterface::STATE_NEW,
$unitsRefunded->paymentMethodId()
$paymentMethod
);

$this->entityManager->persist($refundPayment);
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/app/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sylius_resource:
sylius_refund.refund_payment:
classes:
model: Sylius\RefundPlugin\Entity\RefundPayment
factory: Sylius\RefundPlugin\Factory\RefundPaymentFactory
sylius_refund.customer_billing_data:
classes:
model: Sylius\RefundPlugin\Entity\CustomerBillingData
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/event_bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
<service id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager">
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface" />
<argument type="service" id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface" />
<argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface" />
<argument type="service" id="sylius_refund.factory.refund_payment" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.repository.payment_method" />
<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" />
Expand Down
7 changes: 0 additions & 7 deletions src/Resources/config/services/factories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
<deprecated>The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 1.0, use Sylius\RefundPlugin\Factory\CreditMemoSequenceFactoryInterface instead.</deprecated>
</service>

<service id="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface" class="Sylius\RefundPlugin\Factory\RefundPaymentFactory">
<argument type="service" id="sylius.repository.payment_method" />
</service>
<service id="Sylius\RefundPlugin\Factory\RefundPaymentFactory" alias="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface">
<deprecated>The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 1.0, use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface instead.</deprecated>
</service>

<service
id="Sylius\RefundPlugin\Factory\CreditMemoFactory"
decorates="sylius_refund.factory.credit_memo"
Expand Down

0 comments on commit 7e21428

Please sign in to comment.