Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/EventListener/RenderViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

namespace Spinbits\SyliusGoogleAnalytics4Plugin\EventListener;

use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\RenderHeadTwigFactory;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\RenderHeadTwigFactoryInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class RenderViewListener
{
public function __construct(private RenderHeadTwigFactory $renderTwig)
public function __construct(private RenderHeadTwigFactoryInterface $renderTwig)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Factory/Events/CartEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
use Spinbits\GoogleAnalytics4EventsDtoS\Events\AddToCart;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\RemoveFromCart;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewCart;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Context\CartContextInterface;

class CartEventFactory
{
public function __construct(
private ItemFactory $itemFactory,
private ItemFactoryInterface $itemFactory,
private CartContextInterface $cartContext
) {
}
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/Events/CheckoutEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Spinbits\GoogleAnalytics4EventsDtoS\Events\BeginCheckout;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\Purchase;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\ItemsContainerInterface;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryInterface;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
Expand All @@ -29,7 +29,7 @@ class CheckoutEventFactory
{
public function __construct(
private CartContextInterface $cartContext,
private ItemFactory $itemFactory
private ItemFactoryInterface $itemFactory
) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/Factory/Events/NavigationEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItem;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItemList;
use Spinbits\GoogleAnalytics4EventsDtoS\Events\ItemsContainerInterface;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductInterface as ProdProductInterface;

class NavigationEventFactory
{
public function __construct(private ItemFactory $itemFactory)
public function __construct(private ItemFactoryInterface $itemFactory)
{
}

Expand Down
53 changes: 3 additions & 50 deletions src/Factory/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@

use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\Channel;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderItem;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Core\Model\Taxon;
use Sylius\Component\Order\Model\OrderItemInterface;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductVariantInterface;
Expand All @@ -30,41 +25,14 @@ class ItemFactory
public function __construct(
private ChannelContextInterface $channelContext,
private CurrencyContextInterface $currencyContext,
private ProductVariantResolverInterface $productVariantResolver
private ProductVariantResolverInterface $productVariantResolver,
private ItemFactoryFromProductVariantInterface $itemFactoryFromVariant
) {
}

public function fromProductVariant(ProductVariantInterface $variant): Item
{
/** @var ProductVariant $variant */
/** @var Channel $channel */
$channel = $this->channelContext->getChannel();
/** @var ChannelPricingInterface|null $pricing */
/** @var ProductVariant $variant */
$pricing = $variant->getChannelPricingForChannel($channel);
$price = $pricing?->getPrice() ?? $pricing?->getOriginalPrice() ?? 0;
/** @var Product|null $product */
$product = $variant->getProduct();
/** @var Taxon|null $mainTaxon */
$mainTaxon = $product?->getMainTaxon();

return new Item(
(string) $variant->getId(),
(string) $variant->getCode(),
round($price/100, 2),
$this->currencyContext->getCurrencyCode(),
0,
1,
null,
null,
null,
null,
$this->getCategories($product),
(string) $mainTaxon?->getId(),
(string) $mainTaxon?->getName(),
$variant->getName(),
$channel->getName(),
);
return $this->itemFactoryFromVariant->create($variant);
}

public function fromProduct(ProductInterface $product): Item
Expand Down Expand Up @@ -98,21 +66,6 @@ public function fromOrderItem(OrderItemInterface $orderItem): Item
;
}

private function getCategories(?ProductInterface $product): array
{
/** @var Product|null $product */
if (null === $product) {
return [];
}
$taxons = [];
/** @var Taxon $taxon */
foreach ($product->getTaxons() as $taxon) {
$taxons[] = (string) $taxon->getName();
}

return $taxons;
}

/**
* @param OrderItemInterface $orderItem
* @return array<array-key, string>
Expand Down
79 changes: 79 additions & 0 deletions src/Factory/ItemFactoryFromProductVariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @author Jakub Lech <info@smartbyte.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spinbits\SyliusGoogleAnalytics4Plugin\Factory;

use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\Channel;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Core\Model\Taxon;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductVariantInterface;
use Sylius\Component\Currency\Context\CurrencyContextInterface;

class ItemFactoryFromProductVariant implements ItemFactoryFromProductVariantInterface
{
public function __construct(
private ChannelContextInterface $channelContext,
private CurrencyContextInterface $currencyContext,
) {
}

public function create(ProductVariantInterface $variant): Item
{
/** @var ProductVariant $variant */
/** @var Channel $channel */
$channel = $this->channelContext->getChannel();
/** @var ChannelPricingInterface|null $pricing */
/** @var ProductVariant $variant */
$pricing = $variant->getChannelPricingForChannel($channel);
$price = $pricing?->getPrice() ?? $pricing?->getOriginalPrice() ?? 0;
/** @var Product|null $product */
$product = $variant->getProduct();
/** @var Taxon|null $mainTaxon */
$mainTaxon = $product?->getMainTaxon();

return new Item(
(string) $variant->getId(),
(string) $variant->getCode(),
round($price/100, 2),
$this->currencyContext->getCurrencyCode(),
0,
1,
null,
null,
null,
null,
$this->getCategories($product),
(string) $mainTaxon?->getId(),
(string) $mainTaxon?->getName(),
$variant->getName(),
$channel->getName(),
);
}

private function getCategories(?ProductInterface $product): array
{
/** @var Product|null $product */
if (null === $product) {
return [];
}
$taxons = [];
/** @var Taxon $taxon */
foreach ($product->getTaxons() as $taxon) {
$taxons[] = (string) $taxon->getName();
}

return $taxons;
}
}
19 changes: 19 additions & 0 deletions src/Factory/ItemFactoryFromProductVariantInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author Jakub Lech <info@smartbyte.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spinbits\SyliusGoogleAnalytics4Plugin\Factory;

use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item;
use Sylius\Component\Product\Model\ProductVariantInterface;

interface ItemFactoryFromProductVariantInterface
{
public function create(ProductVariantInterface $variant): Item;
}
21 changes: 21 additions & 0 deletions src/Factory/ItemFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @author Jakub Lech <info@smartbyte.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spinbits\SyliusGoogleAnalytics4Plugin\Factory;

use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item;
use Sylius\Component\Order\Model\OrderItemInterface;
use Sylius\Component\Product\Model\ProductInterface;

interface ItemFactoryInterface
{
public function fromProduct(ProductInterface $product): Item;
public function fromOrderItem(OrderItemInterface $orderItem): Item;
}
2 changes: 1 addition & 1 deletion src/Factory/RenderHeadTwigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Spinbits\SyliusGoogleAnalytics4Plugin\Provider\GoogleTagIdProviderInterface;
use Twig\Environment;

class RenderHeadTwigFactory
class RenderHeadTwigFactory implements RenderHeadTwigFactoryInterface
{
public function __construct(
private GoogleTagIdProviderInterface $googleTagProvider,
Expand Down
16 changes: 16 additions & 0 deletions src/Factory/RenderHeadTwigFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @author Jakub Lech <info@smartbyte.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spinbits\SyliusGoogleAnalytics4Plugin\Factory;

interface RenderHeadTwigFactoryInterface
{
public function render(): string;
}
12 changes: 9 additions & 3 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ services:
- '../../Entity/'
- '../../Kernel.php'

Spinbits\SyliusGoogleAnalytics4Plugin\Factory\RenderHeadTwigFactory:
Spinbits\SyliusGoogleAnalytics4Plugin\Factory\RenderHeadTwigFactoryInterface:
class: Spinbits\SyliusGoogleAnalytics4Plugin\Factory\RenderHeadTwigFactory
arguments:
$enabled: '%spinbits_sylius_google_analytics4.enabled%'
$additionalParams: '%spinbits_sylius_google_analytics4.additionalParameters%'
$templateName: '%spinbits_sylius_google_analytics4.templateName%'

Spinbits\SyliusGoogleAnalytics4Plugin\Provider\GoogleTagIdProvider:
Spinbits\SyliusGoogleAnalytics4Plugin\Provider\GoogleTagIdProviderInterface:
class: Spinbits\SyliusGoogleAnalytics4Plugin\Provider\GoogleTagIdProvider
arguments:
$defaultId: '%spinbits_sylius_google_analytics4.id%'
$channelsIds: '%spinbits_sylius_google_analytics4.channels_ids%'
Expand All @@ -26,10 +28,14 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory:
Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryInterface:
class: Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory
arguments:
$productVariantResolver: '@sylius.product_variant_resolver.default'

Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryFromProductVariantInterface:
class: Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryFromProductVariant

Spinbits\SyliusGoogleAnalytics4Plugin\Factory\EventsBagSessionFactory:
decorates: session.factory
arguments: ['@.inner']
14 changes: 7 additions & 7 deletions tests/Unit/Factory/Events/CartEventFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

use Doctrine\Common\Collections\ArrayCollection;
use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item;
use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\Events\CartEventFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory;
use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Context\CartContextInterface;
Expand All @@ -26,16 +26,16 @@ class CartEventFactoryTest extends TestCase
/** @var CartEventFactory */
private $sut;

/** @var ItemFactory|MockObject */
private ItemFactory $itemFactory;
/** @var ItemFactoryInterface|MockObject */
private ItemFactoryInterface $itemFactory;

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

protected function setUp(): void
{
parent::setUp();
$this->itemFactory = $this->createMock(ItemFactory::class);
$this->itemFactory = $this->createMock(ItemFactoryInterface::class);
$this->cartContext = $this->createMock(CartContextInterface::class);

$item = $this->createMock(Item::class);
Expand All @@ -57,7 +57,7 @@ protected function setUp(): void
}

/** @test */
public function testViewCart()
public function testViewCart(): void
{
$result = $this->sut->viewCart();

Expand All @@ -66,7 +66,7 @@ public function testViewCart()
}

/** @test */
public function testAddToCart()
public function testAddToCart(): void
{
$orderItem = $this->createMock(OrderItemInterface::class);
$result = $this->sut->addToCart($orderItem);
Expand All @@ -76,7 +76,7 @@ public function testAddToCart()
}

/** @test */
public function testRemoveFromCart()
public function testRemoveFromCart(): void
{
$orderItem = $this->createMock(OrderItemInterface::class);
$result = $this->sut->removeFromCart($orderItem);
Expand Down
Loading