Skip to content

Commit

Permalink
refactor #12050 Refactor mailing (lchrusciel, SirDomin)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | not sure
| Related tickets | refactoring the way we handle emails implemented in #11754
| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.7 or 1.8 branch (the lowest possible)
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------

c0d8f32 [API] Order confirmation email sending
85b296b [Core] Extract mailer sender from ShopBundle
a73d321 fix composer.json api platform, tests for api mailing added
24ac912 command added
18818a7 mailing is handled with commands
da13fb2 pr fix, behat + spec fixed
b10b272 pr-fix
0def0d8 introduce event bus
  • Loading branch information
GSadee committed Nov 19, 2020
2 parents b4ef833 + 0def0d8 commit 1192b36
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 15 deletions.
31 changes: 31 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command;

/** @experimental */
class SendOrderConfirmation
{
/** @var string */
public $orderToken;

public function __construct(string $orderToken)
{
$this->orderToken = $orderToken;
}

public function orderToken(): string
{
return $this->orderToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
namespace Sylius\Bundle\ApiBundle\CommandHandler\Checkout;

use SM\Factory\FactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
use Webmozart\Assert\Assert;

/** @experimental */
Expand All @@ -31,17 +34,17 @@ final class CompleteOrderHandler implements MessageHandlerInterface
/** @var FactoryInterface */
private $stateMachineFactory;

/** @var OrderEmailManagerInterface */
private $emailManager;
/** @var MessageBusInterface */
private $eventBus;

public function __construct(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
OrderEmailManagerInterface $emailManager
MessageBusInterface $eventBus
) {
$this->orderRepository = $orderRepository;
$this->stateMachineFactory = $stateMachineFactory;
$this->emailManager = $emailManager;
$this->eventBus = $eventBus;
}

public function __invoke(CompleteOrder $completeOrder): OrderInterface
Expand All @@ -66,7 +69,7 @@ public function __invoke(CompleteOrder $completeOrder): OrderInterface

$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);

$this->emailManager->sendConfirmationEmail($cart);
$this->eventBus->dispatch(new OrderCompleted($cart->getTokenValue()), [new DispatchAfterCurrentBusStamp()]);

return $cart;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\CommandHandler;

use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

/** @experimental */
final class SendOrderConfirmationHandler implements MessageHandlerInterface
{
/** @var SenderInterface */
private $emailSender;

/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(SenderInterface $emailSender, OrderRepositoryInterface $orderRepository)
{
$this->emailSender = $emailSender;
$this->orderRepository = $orderRepository;
}

public function __invoke(SendOrderConfirmation $sendOrderConfirmation): void
{
/** @var OrderInterface $order */
$order = $this->orderRepository->findOneByTokenValue($sendOrderConfirmation->orderToken());

$this->emailSender->send(
Emails::ORDER_CONFIRMATION_RESENT,
[$order->getCustomer()->getEmail()],
[
'order' => $order,
'channel' => $order->getChannel(),
'localeCode' => $order->getLocaleCode(),
]
);
}
}
30 changes: 30 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Event/OrderCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Event;

class OrderCompleted
{
/** @var string */
public $orderToken;

public function __construct(string $orderToken)
{
$this->orderToken = $orderToken;
}

public function orderToken(): string
{
return $this->orderToken;
}
}
35 changes: 35 additions & 0 deletions src/Sylius/Bundle/ApiBundle/EventHandler/OrderCompletedHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\EventHandler;

use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Symfony\Component\Messenger\MessageBusInterface;

/** @experimental */
final class OrderCompletedHandler
{
/** @var MessageBusInterface */
private $commandBus;

public function __construct(MessageBusInterface $commandBus)
{
$this->commandBus = $commandBus;
}

public function __invoke(OrderCompleted $orderCompleted): void
{
$this->commandBus->dispatch(new SendOrderConfirmation($orderCompleted->orderToken()));
}
}
4 changes: 4 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ framework:
middleware:
- 'validation'
- 'doctrine_transaction'
sylius_event.bus:
middleware:
- 'validation'
- 'doctrine_transaction'
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\CompleteOrderHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.mailer.order_email_manager" />
<tag name="messenger.message_handler" />
<argument type="service" id="sylius_event.bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Cart\ChangeItemQuantityInCartHandler">
Expand All @@ -101,5 +101,11 @@
<argument type="service" id="sylius.order_processing.order_processor" />
<tag name="messenger.message_handler" bus="sylius_default.bus"/>
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\SendOrderConfirmationHandler">
<argument type="service" id="sylius.email_sender" />
<argument type="service" id="sylius.repository.order" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="Sylius\Bundle\ApiBundle\EventHandler\OrderCompletedHandler">
<argument type="service" id="message_bus" />
<argument type="service" id="sylius.repository.order" />
<tag name="messenger.message_handler" bus="sylius_event.bus"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;

final class CompleteOrderHandlerSpec extends ObjectBehavior
{
function let(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
OrderEmailManagerInterface $emailManager
MessageBusInterface $eventBus
): void {
$this->beConstructedWith($orderRepository, $stateMachineFactory, $emailManager);
$this->beConstructedWith($orderRepository, $stateMachineFactory, $eventBus);
}

function it_handles_order_completion_without_notes(
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory,
OrderEmailManagerInterface $emailManager
MessageBusInterface $eventBus
): void {
$completeOrder = new CompleteOrder();
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -52,7 +55,13 @@ function it_handles_order_completion_without_notes(

$stateMachine->apply('complete')->shouldBeCalled();

$emailManager->sendConfirmationEmail($order)->shouldBeCalled();
$orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN');

$eventBus
->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()])
->willReturn(new Envelope($orderCompleted))
->shouldBeCalled()
;

$this($completeOrder)->shouldReturn($order);
}
Expand All @@ -62,7 +71,7 @@ function it_handles_order_completion_with_notes(
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory,
OrderEmailManagerInterface $emailManager
MessageBusInterface $eventBus
): void {
$completeOrder = new CompleteOrder('ThankYou');
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -77,7 +86,13 @@ function it_handles_order_completion_with_notes(

$stateMachine->apply('complete')->shouldBeCalled();

$emailManager->sendConfirmationEmail($order)->shouldBeCalled();
$orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN');

$eventBus
->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()])
->willReturn(new Envelope($orderCompleted))
->shouldBeCalled()
;

$this($completeOrder)->shouldReturn($order);
}
Expand Down
Loading

0 comments on commit 1192b36

Please sign in to comment.