Skip to content

Commit

Permalink
fix and rename listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Apr 13, 2021
1 parent 7aaf9d3 commit dfa74f8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 32 deletions.
2 changes: 1 addition & 1 deletion UPGRADE-1.10.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# UPGRADE FROM `v1.9.X` TO `v1.10.0`

1. CartBlamerListener has been moved from CoreBundle to ShopBundle and adjusted to work properly when decoupled.
1. `Sylius\Bundle\CoreBundle\EventListener\CartBlamerListener` has been moved from CoreBundle to ShopBundle, renamed to `Sylius\Bundle\ShopBundle\EventListener\ShopCartBlamerListener` and adjusted to work properly when decoupled.

### New API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sylius\Bundle\ApiBundle\EventListener;

use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiOrdersSubSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Bundle\UserBundle\Event\UserEvent;
Expand All @@ -24,23 +23,18 @@
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

final class CartBlamerListener
final class ApiCartBlamerListener
{
/** @var ObjectManager */
private $cartManager;

/** @var CartContextInterface */
private $cartContext;

/** @var SectionProviderInterface */
private $uriBasedSectionContext;

public function __construct(
ObjectManager $cartManager,
CartContextInterface $cartContext,
SectionProviderInterface $uriBasedSectionContext
) {
$this->cartManager = $cartManager;
$this->cartContext = $cartContext;
$this->uriBasedSectionContext = $uriBasedSectionContext;
}
Expand Down Expand Up @@ -76,13 +70,11 @@ public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent)
private function blame(ShopUserInterface $user): void
{
$cart = $this->getCart();
if (null === $cart) {
if (null === $cart || null !== $cart->getCustomer()) {
return;
}

$cart->setCustomer($user->getCustomer());
$this->cartManager->persist($cart);
$this->cartManager->flush();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@
<tag name="sylius.uri_based_section_resolver" priority="40" />
</service>

<service id="sylius.listener.cart_blamer" class="Sylius\Bundle\ApiBundle\EventListener\CartBlamerListener">
<argument type="service" id="sylius.manager.order" />
<service id="sylius.listener.api_cart_blamer" class="Sylius\Bundle\ApiBundle\EventListener\ApiCartBlamerListener">
<argument type="service" id="sylius.context.cart" />
<argument type="service" id="sylius.section_resolver.uri_based_section_resolver" />
<tag name="kernel.event_listener" event="sylius.user.security.implicit_login" method="onImplicitLogin" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function __construct(string $shopApiUriBeginning, string $shopApiOrdersRe

public function getSection(string $uri): SectionInterface
{
if (0 === strpos($uri, $this->shopApiUriBeginning)) {
if (str_contains($uri, $this->shopApiOrdersResourceUri)) {
return new ShopApiOrdersSubSection();
}
if (0 !== strpos($uri, $this->shopApiUriBeginning)) {
throw new SectionCannotBeResolvedException();
}

return new ShopApiSection();
if (str_contains($uri, $this->shopApiOrdersResourceUri)) {
return new ShopApiOrdersSubSection();
}

throw new SectionCannotBeResolvedException();
return new ShopApiSection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\Bundle\ApiBundle\SectionResolver;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiOrdersSubSection;
use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionCannotBeResolvedException;
use Sylius\Bundle\CoreBundle\SectionResolver\UriBasedSectionResolverInterface;
Expand All @@ -36,6 +37,11 @@ function it_returns_shop_api_section_if_path_starts_with_api_v2_shop(): void
$this->getSection('/api/v2/shop')->shouldBeLike(new ShopApiSection());
}

function it_returns_shop_api_orders_subsection_if_path_contains_orders(): void
{
$this->getSection('/api/v2/shop/orders')->shouldBeLike(new ShopApiOrdersSubSection());
}

function it_throws_an_exception_if_path_does_not_start_with_api_v2_shop(): void
{
$this->shouldThrow(SectionCannotBeResolvedException::class)->during('getSection', ['/shop']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sylius\Bundle\ShopBundle\EventListener;

use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Bundle\ShopBundle\SectionResolver\ShopSection;
use Sylius\Bundle\UserBundle\Event\UserEvent;
Expand All @@ -24,23 +23,18 @@
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

final class CartBlamerListener
final class ShopCartBlamerListener
{
/** @var ObjectManager */
private $cartManager;

/** @var CartContextInterface */
private $cartContext;

/** @var SectionProviderInterface */
private $uriBasedSectionContext;

public function __construct(
ObjectManager $cartManager,
CartContextInterface $cartContext,
SectionProviderInterface $uriBasedSectionContext
) {
$this->cartManager = $cartManager;
$this->cartContext = $cartContext;
$this->uriBasedSectionContext = $uriBasedSectionContext;
}
Expand Down Expand Up @@ -77,13 +71,11 @@ public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent)
private function blame(ShopUserInterface $user): void
{
$cart = $this->getCart();
if (null === $cart) {
if (null === $cart || null !== $cart->getCustomer()) {
return;
}

$cart->setCustomer($user->getCustomer());
$this->cartManager->persist($cart);
$this->cartManager->flush();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<services>
<defaults public="true" />

<service id="sylius.listener.cart_blamer" class="Sylius\Bundle\ShopBundle\EventListener\CartBlamerListener">
<argument type="service" id="sylius.manager.order" />
<service id="sylius.listener.shop_cart_blamer" class="Sylius\Bundle\ShopBundle\EventListener\ShopCartBlamerListener">
<argument type="service" id="sylius.context.cart" />
<argument type="service" id="sylius.section_resolver.uri_based_section_resolver" />
<tag name="kernel.event_listener" event="sylius.user.security.implicit_login" method="onImplicitLogin" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

final class CartBlamerListenerSpec extends ObjectBehavior
final class ShopCartBlamerListenerSpec extends ObjectBehavior
{
function let(
ObjectManager $cartManager,
Expand Down

0 comments on commit dfa74f8

Please sign in to comment.