Skip to content

Commit

Permalink
mailing is handled with commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Nov 17, 2020
1 parent 24ac912 commit 18818a7
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 39 deletions.
21 changes: 0 additions & 21 deletions src/Sylius/Bundle/ApiBundle/Command/OrderEmailSenderInterface.php

This file was deleted.

32 changes: 32 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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;

use Sylius\Component\Order\Model\OrderInterface;

class SendOrderConfirmation
{
/** @var OrderInterface */
protected $order;

public function __construct(OrderInterface $order)
{
$this->order = $order;
}

public function order(): OrderInterface
{
return $this->order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

use SM\Factory\FactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface;
use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent;
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 +33,17 @@ final class CompleteOrderHandler implements MessageHandlerInterface
/** @var FactoryInterface */
private $stateMachineFactory;

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

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

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

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

$this->emailManager->sendConfirmationEmail($cart);
$this->messageBus->dispatch(new OrderCompletedEvent($orderTokenValue), [new DispatchAfterCurrentBusStamp()]);

return $cart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command;
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\Mailer\Sender\SenderInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class OrderEmailSender implements OrderEmailSenderInterface
/** @experimental */
final class SendOrderConfirmationHandler implements MessageHandlerInterface
{
/** @var SenderInterface */
private $emailSender;
Expand All @@ -27,8 +29,10 @@ public function __construct(SenderInterface $emailSender)
$this->emailSender = $emailSender;
}

public function sendConfirmationEmail(OrderInterface $order): void
public function __invoke(SendOrderConfirmation $sendOrderConfirmation): void
{
$order = $sendOrderConfirmation->order();

$this->emailSender->send(
Emails::ORDER_CONFIRMATION_RESENT,
[$order->getCustomer()->getEmail()],
Expand Down
30 changes: 30 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Event/OrderCompletedEvent.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;

final class OrderCompletedEvent
{
/** @var string */
protected $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
@@ -0,0 +1,42 @@
<?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\EventListener;

use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\OrderRepository;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class OrderCompleteListener
{
/** @var MessageBusInterface */
private $bus;

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

public function __construct(MessageBusInterface $bus, OrderRepository $orderRepository)
{
$this->bus = $bus;
$this->orderRepository = $orderRepository;
}

public function __invoke(OrderCompletedEvent $orderCompleted): void
{
$order = $this->orderRepository->findOneByTokenValue($orderCompleted->orderToken());

$this->bus->dispatch(new Envelope(new SendOrderConfirmation($order)));
}
}
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="api_platform.message_bus" />
<tag name="messenger.command_handler" bus="sylius_default.bus" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Cart\ChangeItemQuantityInCartHandler">
Expand All @@ -101,5 +101,10 @@
<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" />
<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_core.event_listener.order_complete_listener" class="Sylius\Bundle\ApiBundle\EventListener\OrderCompleteListener">
<argument type="service" id="message_bus" />
<argument type="service" id="sylius.repository.order" />
<tag name="messenger.message_handler" bus="sylius_default.bus"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/Sylius/Bundle/ApiBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.3",
"api-platform/core": "^2.5",
"sylius/core-bundle": "dev-master",
"sylius/core-bundle": "^1.7",
"symfony/messenger": "^4.4"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,34 @@
namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Checkout;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent;
use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
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 $messageBus
): void {
$this->beConstructedWith($orderRepository, $stateMachineFactory, $emailManager);
$this->beConstructedWith($orderRepository, $stateMachineFactory, $messageBus);
}

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

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

$emailManager->sendConfirmationEmail($order)->shouldBeCalled();

$this($completeOrder)->shouldReturn($order);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 spec\Sylius\Bundle\ApiBundle\CommandHandler;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class SendOrderConfirmationHandlerSpec extends ObjectBehavior
{
function let(
SenderInterface $sender
): void {
$this->beConstructedWith($sender);
}

function it_is_a_message_handler(): void
{
$this->shouldImplement(MessageHandlerInterface::class);
}

function it_sends_order_confirmation_message(
OrderInterface $order,
SendOrderConfirmation $sendOrderConfirmation,
SenderInterface $sender,
CustomerInterface $customer,
ChannelInterface $channel
): void {
$sendOrderConfirmation->order()->willReturn($order);

$order->getChannel()->willReturn($channel);
$order->getLocaleCode()->willReturn('pl_PL');

$order->getCustomer()->willReturn($customer);
$customer->getEmail()->willReturn('johnny.bravo@email.com');

$sender->send(
Emails::ORDER_CONFIRMATION_RESENT,
['johnny.bravo@email.com'],
[
'order' => $order->getWrappedObject(),
'channel' => $channel->getWrappedObject(),
'localeCode' => 'pl_PL',
]
)->shouldBeCalled();

$this(new SendOrderConfirmation($order->getWrappedObject()));
}
}

0 comments on commit 18818a7

Please sign in to comment.