Skip to content

Commit

Permalink
Change the way notifications are created and associated with an order…
Browse files Browse the repository at this point in the history
…/cart
  • Loading branch information
loevgaard committed Oct 18, 2022
1 parent c2891b6 commit ce80fcc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusAbandonedCartPlugin\EventListener\Doctrine;

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\ManagerRegistry;
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait;
use Setono\SyliusAbandonedCartPlugin\Factory\NotificationFactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;

final class CreateNotificationOnOrderPersistenceListener
{
use ORMManagerTrait;

private NotificationFactoryInterface $notificationFactory;

public function __construct(
NotificationFactoryInterface $notificationFactory,
ManagerRegistry $managerRegistry
) {
$this->notificationFactory = $notificationFactory;
$this->managerRegistry = $managerRegistry;
}

public function prePersist(LifecycleEventArgs $eventArgs): void
{
$obj = $eventArgs->getObject();
if (!$obj instanceof OrderInterface) {
return;
}

$notification = $this->notificationFactory->createWithCart($obj);

$manager = $this->getManager($notification);
$manager->persist($notification);
}
}
25 changes: 3 additions & 22 deletions src/Factory/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,26 @@

namespace Setono\SyliusAbandonedCartPlugin\Factory;

use Doctrine\Persistence\ManagerRegistry;
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\TokenAssigner\OrderTokenAssignerInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

/**
* This factory decorates the order factory and creates a notification each time a new order is created.
* Also, it sets the token on the order because we need this token if we want to reinstate a cart to a customer.
*
* If the order is persisted and flushed, the notification will also be persisted
* This factory decorates the order factory to set the token on the order
* because we need this token if we want to reinstate a cart to a customer.
*/
final class OrderFactory implements FactoryInterface
{
use ORMManagerTrait;

private FactoryInterface $decorated;

private OrderTokenAssignerInterface $orderTokenAssigner;

private NotificationFactoryInterface $notificationFactory;

public function __construct(
FactoryInterface $decorated,
OrderTokenAssignerInterface $orderTokenAssigner,
NotificationFactoryInterface $notificationFactory,
ManagerRegistry $managerRegistry
OrderTokenAssignerInterface $orderTokenAssigner
) {
$this->decorated = $decorated;
$this->orderTokenAssigner = $orderTokenAssigner;
$this->notificationFactory = $notificationFactory;
$this->managerRegistry = $managerRegistry;
}

public function createNew(): object
Expand All @@ -44,13 +32,6 @@ public function createNew(): object

if ($order instanceof OrderInterface) {
$this->orderTokenAssigner->assignTokenValueIfNotSet($order);

$notification = $this->notificationFactory->createWithCart($order);

$manager = $this->getManager($notification);
if ($manager->isOpen()) {
$manager->persist($notification);
}
}

return $order;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<import resource="services/controller.xml"/>
<import resource="services/dispatcher.xml"/>
<import resource="services/eligibility_checker.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/event_subscriber.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/form.xml"/>
Expand Down
15 changes: 15 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_abandoned_cart.event_listener.doctrine.create_notification_on_order_persistence"
class="Setono\SyliusAbandonedCartPlugin\EventListener\Doctrine\CreateNotificationOnOrderPersistenceListener">
<argument type="service" id="setono_sylius_abandoned_cart.factory.notification"/>
<argument type="service" id="doctrine"/>

<tag name="doctrine.event_listener" event="prePersist"/>
</service>
</services>
</container>
2 changes: 0 additions & 2 deletions src/Resources/config/services/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
decorates="sylius.factory.order">
<argument type="service" id="setono_sylius_abandoned_cart.custom_factory.order.inner"/>
<argument type="service" id="sylius.unique_id_based_order_token_assigner"/>
<argument type="service" id="setono_sylius_abandoned_cart.factory.notification"/>
<argument type="service" id="doctrine"/>
</service>
</services>
</container>
16 changes: 1 addition & 15 deletions tests/Factory/OrderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

namespace Tests\Setono\SyliusAbandonedCartPlugin\Factory;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusAbandonedCartPlugin\Factory\NotificationFactory;
use Setono\SyliusAbandonedCartPlugin\Factory\OrderFactory;
use Setono\SyliusAbandonedCartPlugin\Model\Notification;
use Setono\SyliusAbandonedCartPlugin\Model\NotificationInterface;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\TokenAssigner\OrderTokenAssignerInterface;
Expand All @@ -33,18 +28,9 @@ public function it_creates_notification_when_creating_order(): void
$orderTokenAssigner = $this->prophesize(OrderTokenAssignerInterface::class);
$orderTokenAssigner->assignTokenValueIfNotSet(Argument::type(OrderInterface::class))->shouldBeCalled();

$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->isOpen()->willReturn(true);
$entityManager->persist(Argument::type(NotificationInterface::class))->shouldBeCalled();

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Notification::class)->willReturn($entityManager->reveal());

$factory = new OrderFactory(
new Factory(Order::class),
$orderTokenAssigner->reveal(),
new NotificationFactory(new Factory(Notification::class)),
$managerRegistry->reveal()
$orderTokenAssigner->reveal()
);
$factory->createNew();
}
Expand Down

0 comments on commit ce80fcc

Please sign in to comment.