Skip to content

Commit

Permalink
PISHPS-311: SubscriptionCartCollector now uses LineItemAttributes to …
Browse files Browse the repository at this point in the history
…determine if a product is a subscription product
  • Loading branch information
m-muxfeld-diw committed Aug 12, 2024
1 parent 9e1fa93 commit ac7bb09
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionProductIdentifier"/>


<service id="Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionCartCollector">
<tag name="shopware.cart.collector" priority="1999" />
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionProductIdentifier"/>
</service>


Expand Down
31 changes: 9 additions & 22 deletions src/Service/Cart/Subscription/SubscriptionCartCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Shopware\Core\Checkout\Cart\CartBehavior;
use Shopware\Core\Checkout\Cart\CartDataCollectorInterface;
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
use Shopware\Core\Checkout\Cart\LineItem\LineItem as CheckoutCartLineItem;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand All @@ -20,21 +19,26 @@
*/
class SubscriptionCartCollector implements CartDataCollectorInterface
{
private const SUBSCRIPTION_ENABLED = 'mollie_payments_product_subscription_enabled';

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* @var SubscriptionProductIdentifier
*/
private $subscriptionProductIdentifier;

/**
* SubscriptionCartCollector constructor.
*
* @param EventDispatcherInterface $dispatcher The event dispatcher
* @param SubscriptionProductIdentifier $subscriptionProductIdentifier
*/
public function __construct(EventDispatcherInterface $dispatcher)
public function __construct(EventDispatcherInterface $dispatcher, SubscriptionProductIdentifier $subscriptionProductIdentifier)
{
$this->dispatcher = $dispatcher;
$this->subscriptionProductIdentifier = $subscriptionProductIdentifier;
}

/**
Expand All @@ -49,27 +53,10 @@ public function collect(CartDataCollection $data, Cart $original, SalesChannelCo
{
$events = [];
foreach ($original->getLineItems() as $lineItem) {
if ($this->lineItemIsSubscriptionProduct($lineItem)) {
if ($this->subscriptionProductIdentifier->isSubscriptionProduct($lineItem)) {
$events[] = new MollieSubscriptionCartItemAddedEvent($context, $lineItem);
}
}
array_map([$this->dispatcher, 'dispatch'], $events);
}

/**
* Checks if a line item is a subscription product.
*
* @param CheckoutCartLineItem $lineItem The line item to check
* @return bool True if the line item is a subscription product, false otherwise
*/
private function lineItemIsSubscriptionProduct(CheckoutCartLineItem $lineItem): bool
{
$customFields = $lineItem->getPayloadValue('customFields');

if (is_array($customFields) === false || isset($customFields[self::SUBSCRIPTION_ENABLED]) === false) {
return false;
}

return (bool)$customFields[self::SUBSCRIPTION_ENABLED];
}
}
21 changes: 21 additions & 0 deletions src/Service/Cart/Subscription/SubscriptionProductIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Service\Cart\Subscription;

use Kiener\MolliePayments\Struct\LineItem\LineItemAttributes;
use Shopware\Core\Checkout\Cart\LineItem\LineItem as CheckoutCartLineItem;

class SubscriptionProductIdentifier
{
/**
* Checks if a line item is a subscription product.
*
* @param CheckoutCartLineItem $lineItem
* @return bool
*/
public function isSubscriptionProduct(CheckoutCartLineItem $lineItem): bool
{
return (new LineItemAttributes($lineItem))->isSubscriptionProduct();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Kiener\MolliePayments\Event\MollieSubscriptionCartItemAddedEvent;
use Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionCartCollector;
use Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionProductIdentifier;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartBehavior;
Expand All @@ -19,19 +20,23 @@ class SubscriptionCartCollectorTest extends TestCase
{
const SUBSCRIPTION_ENABLED = 'mollie_payments_product_subscription_enabled';
private $dispatcher;

private $identifier;
private $collector;

private $data;

protected function setUp(): void
{
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->collector = new SubscriptionCartCollector($this->dispatcher);
$this->identifier = $this->createMock(SubscriptionProductIdentifier::class);
$this->collector = new SubscriptionCartCollector($this->dispatcher, $this->identifier);
$this->data = $this->createMock(CartDataCollection::class);
$this->original = $this->createMock(Cart::class);
$this->context = $this->createMock(SalesChannelContext::class);
$this->behavior = $this->createMock(CartBehavior::class);

$this->identifier->method('isSubscriptionProduct')->willReturnCallback(function (CheckoutCartLineItem $lineItem) {
return $lineItem->getPayloadValue(self::SUBSCRIPTION_ENABLED)[self::SUBSCRIPTION_ENABLED] ?? false;
});
}

public function testDispatchesEventWhenAProductIsAMollieSubscriptionProduct(): void
Expand Down Expand Up @@ -65,18 +70,6 @@ public function testDoesNotDispatchEventWhenNoMollieSubscriptionProductIsAdded()
$this->collector->collect($this->data, $this->original, $this->context, $this->behavior);
}

public function testDoesNotDispatchEventWhenCustomFieldsIsMissingSubscriptionData(): void
{
$incorrectlyConfiguredProduct = $this->createLineItemMockWithPayloadValue((object)[self::SUBSCRIPTION_ENABLED => false,]);

$this->configureGetLineItemsMethodOfCart($incorrectlyConfiguredProduct);

// we expect the event to not be dispatched
$this->dispatcher->expects($this->never())->method('dispatch');

$this->collector->collect($this->data, $this->original, $this->context, $this->behavior);
}

private function createLineItemMockWithPayloadValue($value): CheckoutCartLineItem
{
return $this->createConfiguredMock(
Expand Down

0 comments on commit ac7bb09

Please sign in to comment.