Skip to content

Commit

Permalink
Merge pull request #76 from Zales0123/refactor-refund-payment-creation
Browse files Browse the repository at this point in the history
🌊
  • Loading branch information
bartoszpietrzak1994 authored Sep 28, 2018
2 parents 74c0fbb + 1f297da commit 2702b2d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 33 deletions.
21 changes: 21 additions & 0 deletions spec/Event/RefundPaymentGeneratedSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Event;

use PhpSpec\ObjectBehavior;

final class RefundPaymentGeneratedSpec extends ObjectBehavior
{
function it_represents_an_immutable_fact_that_refund_payment_has_been_generated(): void
{
$this->beConstructedWith(1, '000222', 10000, 'GBP', 2);

$this->id()->shouldReturn(1);
$this->orderNumber()->shouldReturn('000222');
$this->amount()->shouldReturn(10000);
$this->currencyCode()->shouldReturn('GBP');
$this->paymentMethodId()->shouldReturn(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,68 @@

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Listener;
namespace spec\Sylius\RefundPlugin\ProcessManager;

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Prooph\ServiceBus\EventBus;
use Prophecy\Argument;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;

final class UnitsRefundedEventListenerSpec extends ObjectBehavior
final class RefundPaymentProcessManagerSpec extends ObjectBehavior
{
function let(
Session $session,
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RefundPaymentFactoryInterface $refundPaymentFactory,
EntityManagerInterface $entityManager
EntityManagerInterface $entityManager,
EventBus $eventBus
): void {
$this->beConstructedWith(
$session,
$orderFullyRefundedStateResolver,
$refundPaymentFactory,
$entityManager
$entityManager,
$eventBus
);
}

function it_listens_to_units_refunded_event_and_add_success_flash_after_it_occurs(
Session $session,
FlashBagInterface $flashBag,
function it_reacts_on_units_refunded_event_and_creates_refund_payment(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RefundPaymentFactoryInterface $refundPaymentFactory,
EntityManagerInterface $entityManager,
RefundPaymentInterface $refundPayment
RefundPaymentInterface $refundPayment,
EventBus $eventBus
): void {
$refundPaymentFactory->createWithData(
'000222',
1000,
'USD',
RefundPaymentInterface::STATE_NEW, 1
RefundPaymentInterface::STATE_NEW,
1
)->willReturn($refundPayment);

$entityManager->persist($refundPayment)->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();

$orderFullyRefundedStateResolver->resolve('000222')->shouldBeCalled();

$session->getFlashBag()->willReturn($flashBag);
$refundPayment->getId()->willReturn(10);
$refundPayment->getOrderNumber()->willReturn('000222');
$refundPayment->getAmount()->willReturn(1000);

$flashBag->add('success', 'sylius_refund.units_successfully_refunded')->shouldBeCalled();
$eventBus->dispatch(Argument::that(function (RefundPaymentGenerated $event): bool {
return
$event->id() === 10 &&
$event->orderNumber() === '000222' &&
$event->amount() === 1000 &&
$event->currencyCode() === 'USD' &&
$event->paymentMethodId() === 1
;
}))->shouldBeCalled();

$this(new UnitsRefunded('000222', [new OrderItemUnitRefund(1, 500), new OrderItemUnitRefund(2, 500)], [1], 1, 1000, 'USD', 'Comment'));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function __invoke(Request $request): Response
{
try {
$this->commandBus->dispatch($this->commandCreator->fromRequest($request));

$this->session->getFlashBag()->add('success', 'sylius_refund.units_successfully_refunded');
} catch (CommandDispatchException $exception) {
$this->session->getFlashBag()->add('error', $exception->getPrevious()->getMessage());
} catch (\InvalidArgumentException $exception) {
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/RefundPaymentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface RefundPaymentInterface extends ResourceInterface
public const STATE_NEW = 'New';
public const STATE_COMPLETED = 'Completed';

public function getOrderNumber(): string;

public function getAmount(): int;

public function getCurrencyCode(): string;
Expand Down
57 changes: 57 additions & 0 deletions src/Event/RefundPaymentGenerated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Sylius\RefundPlugin\Event;

final class RefundPaymentGenerated
{
/** @var int */
private $id;

/** @var string */
private $orderNumber;

/** @var int */
private $amount;

/** @var string */
private $currencyCode;

/** @var int */
private $paymentMethodId;

public function __construct(int $id, string $orderNumber, int $amount, string $currencyCode, int $paymentMethodId)
{
$this->id = $id;
$this->orderNumber = $orderNumber;
$this->amount = $amount;
$this->currencyCode = $currencyCode;
$this->paymentMethodId = $paymentMethodId;
}

public function id(): int
{
return $this->id;
}

public function orderNumber(): string
{
return $this->orderNumber;
}

public function amount(): int
{
return $this->amount;
}

public function currencyCode(): string
{
return $this->currencyCode;
}

public function paymentMethodId(): int
{
return $this->paymentMethodId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

declare(strict_types=1);

namespace Sylius\RefundPlugin\Listener;
namespace Sylius\RefundPlugin\ProcessManager;

use Doctrine\ORM\EntityManagerInterface;
use Prooph\ServiceBus\EventBus;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface;
use Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface;
use Symfony\Component\HttpFoundation\Session\Session;

final class UnitsRefundedEventListener
final class RefundPaymentProcessManager
{
/** @var Session */
private $session;

/** @var OrderFullyRefundedStateResolverInterface */
private $orderFullyRefundedStateResolver;

Expand All @@ -25,16 +23,19 @@ final class UnitsRefundedEventListener
/** @var EntityManagerInterface */
private $entityManager;

/** @var EventBus */
private $eventBus;

public function __construct(
Session $session,
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RefundPaymentFactoryInterface $refundPaymentFactory,
EntityManagerInterface $entityManager
EntityManagerInterface $entityManager,
EventBus $eventBus
) {
$this->session = $session;
$this->orderFullyRefundedStateResolver = $orderFullyRefundedStateResolver;
$this->refundPaymentFactory = $refundPaymentFactory;
$this->entityManager = $entityManager;
$this->eventBus = $eventBus;
}

public function __invoke(UnitsRefunded $event): void
Expand All @@ -50,7 +51,14 @@ public function __invoke(UnitsRefunded $event): void
$this->entityManager->persist($refundPayment);
$this->entityManager->flush();

$this->eventBus->dispatch(new RefundPaymentGenerated(
$refundPayment->getId(),
$event->orderNumber(),
$event->amount(),
$event->currencyCode(),
$event->paymentMethodId()
));

$this->orderFullyRefundedStateResolver->resolve($event->orderNumber());
$this->session->getFlashBag()->add('success', 'sylius_refund.units_successfully_refunded');
}
}
16 changes: 8 additions & 8 deletions src/Resources/config/services/event_bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@

<service id="Prooph\ServiceBus\EventBus" alias="prooph_service_bus.sylius_refund_event_bus" />

<service id="Sylius\RefundPlugin\Listener\UnitsRefundedEventListener">
<argument type="service" id="session" />
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolver" />
<argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactory" />
<argument type="service" id="doctrine.orm.default_entity_manager" />
<tag name="prooph_service_bus.sylius_refund_event_bus.route_target" message-detection="true" />
</service>

<service id="Sylius\RefundPlugin\Listener\CreditMemoGeneratedEventListener">
<argument type="service" id="sylius_refund.repository.credit_memo" />
<argument type="service" id="sylius.repository.order" />
Expand All @@ -25,5 +17,13 @@
<argument type="service" id="prooph_service_bus.sylius_refund_command_bus" />
<tag name="prooph_service_bus.sylius_refund_event_bus.route_target" message-detection="true" />
</service>

<service id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager">
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolver" />
<argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactory" />
<argument type="service" id="doctrine.orm.default_entity_manager" />
<argument type="service" id="prooph_service_bus.sylius_refund_event_bus" />
<tag name="prooph_service_bus.sylius_refund_event_bus.route_target" message-detection="true" />
</service>
</services>
</container>

0 comments on commit 2702b2d

Please sign in to comment.