Skip to content

Commit

Permalink
Allow to send product variants instead of products
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Oct 22, 2024
1 parent feadb8a commit b6ef99c
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 14 deletions.
6 changes: 1 addition & 5 deletions config/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
->deprecate('webgriffe/sylius-clerk-plugin', '3.0', 'The "%service_id%" service is deprecated and will be removed in 4.0.')
;

$services->set('webgriffe_sylius_clerk_plugin.provider.products', QueryBuilderResourceProvider::class)
->args([
service('webgriffe_sylius_clerk_plugin.query_builder.products'),
])
;
$services->set('webgriffe_sylius_clerk_plugin.provider.products', QueryBuilderResourceProvider::class);

$services->set('webgriffe_sylius_clerk_plugin.provider.categories', QueryBuilderResourceProvider::class)
->args([
Expand Down
8 changes: 8 additions & 0 deletions config/services/query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM\CustomersQueryBuilder;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM\OrdersQueryBuilder;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM\ProductsQueryBuilder;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM\ProductVariantsQueryBuilder;
use Webgriffe\SyliusClerkPlugin\QueryBuilder\CustomersQueryBuilderFactory;
use Webgriffe\SyliusClerkPlugin\QueryBuilder\OrdersQueryBuilderFactory;
use Webgriffe\SyliusClerkPlugin\QueryBuilder\ProductsQueryBuilderFactory;
Expand Down Expand Up @@ -51,6 +52,13 @@
])
;

$services->set('webgriffe_sylius_clerk_plugin.query_builder.product_variants', ProductVariantsQueryBuilder::class)
->args([
service('sylius.repository.product_variant'),
service('event_dispatcher'),
])
;

$services->set('webgriffe_sylius_clerk_plugin.query_builder.categories', CategoriesQueryBuilder::class)
->args([
service('sylius.repository.taxon'),
Expand Down
11 changes: 11 additions & 0 deletions config/services/serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\CustomerNormalizer as V2CustomerNormalizer;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\OrderNormalizer as V2OrderNormalizer;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\ProductNormalizer as V2ProductNormalizer;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\ProductVariantNormalizer;
use Webgriffe\SyliusClerkPlugin\Normalizer\CustomerNormalizer;
use Webgriffe\SyliusClerkPlugin\Normalizer\OrderNormalizer;
use Webgriffe\SyliusClerkPlugin\Normalizer\ProductNormalizer;
Expand Down Expand Up @@ -60,6 +61,16 @@
->tag('serializer.normalizer', ['priority' => 100])
;

$services->set('webgriffe_sylius_clerk_plugin.normalizer.product_variant', ProductVariantNormalizer::class)
->args([
'$eventDispatcher' => service('event_dispatcher'),
'$productVariantPricesCalculator' => service('sylius.calculator.product_variant_price'),
'$urlGenerator' => service('router'),
'$cacheManager' => service('liip_imagine.cache.manager'),
])
->tag('serializer.normalizer', ['priority' => 100])
;

$services->set('webgriffe_sylius_clerk_plugin.normalizer.category', CategoryNormalizer::class)
->args([
'$eventDispatcher' => service('event_dispatcher'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getResult(
}

$this->eventDispatcher->dispatch(new QueryBuilderEvent(
$this->getResource(),
$queryBuilder,
$channel,
$localeCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getResult(
}

$this->eventDispatcher->dispatch(new QueryBuilderEvent(
$this->getResource(),
$queryBuilder,
$channel,
$localeCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use Doctrine\ORM\QueryBuilder;
use Sylius\Component\Core\Model\ChannelInterface;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Enum\Resource;

final readonly class QueryBuilderEvent
{
public function __construct(
private Resource $resource,
private QueryBuilder $queryBuilder,
private ChannelInterface $channel,
private string $localeCode,
Expand All @@ -19,6 +21,11 @@ public function __construct(
) {
}

public function getResource(): Resource
{
return $this->resource;
}

public function getQueryBuilder(): QueryBuilder
{
return $this->queryBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getResult(
}

$this->eventDispatcher->dispatch(new QueryBuilderEvent(
$this->getResource(),
$queryBuilder,
$channel,
$localeCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM;

use Psr\EventDispatcher\EventDispatcherInterface;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductVariantRepository;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Doctrine\ORM\Event\QueryBuilderEvent;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Enum\Resource;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Model\QueryBuilderInterface;

/**
* @implements QueryBuilderInterface<ProductVariantInterface>
*/
final readonly class ProductVariantsQueryBuilder implements QueryBuilderInterface
{
public function __construct(
private ProductVariantRepository $productVariantRepository,
private EventDispatcherInterface $eventDispatcher,
) {
}

public function getResource(): Resource
{
return Resource::PRODUCTS;
}

public function getResult(
ChannelInterface $channel,
string $localeCode,
?\DateTimeInterface $modifiedAfter = null,
?int $limit = null,
?int $offset = null,
): array {
$queryBuilder = $this->productVariantRepository->createQueryBuilder('pv');

$queryBuilder
->join('pv.product', 'p')
->andWhere('pv.enabled = true')
->leftJoin('pv.translations', 'pvt', 'WITH', 'pvt.locale = :localeCode')
->setParameter('localeCode', $localeCode)
;
$queryBuilder
->andWhere(':channel MEMBER OF p.channels')
->andWhere('p.enabled = true')
->setParameter('channel', $channel)
->leftJoin('p.translations', 'pt', 'WITH', 'pt.locale = :localeCode')
;

if ($modifiedAfter !== null) {
$queryBuilder
->andWhere('pv.updatedAt > :modifiedAfter')
->setParameter('modifiedAfter', $modifiedAfter)
;
}

if ($limit !== null) {
$queryBuilder->setMaxResults($limit);
}

if ($offset !== null) {
$queryBuilder->setFirstResult($offset);
}

$this->eventDispatcher->dispatch(new QueryBuilderEvent(
$this->getResource(),
$queryBuilder,
$channel,
$localeCode,
$modifiedAfter,
$limit,
$offset,
));

/** @var ProductVariantInterface[] $result */
$result = $queryBuilder->getQuery()->getResult();

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function getResult(
}

$this->eventDispatcher->dispatch(new QueryBuilderEvent(
$this->getResource(),
$queryBuilder,
$channel,
$localeCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\Event;

use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

final class ProductVariantNormalizerEvent
{
/**
* @param array{
* id: string|int,
* name: string,
* description?: string,
* price: float,
* list_price: float,
* image?: string,
* url: string,
* categories: array<int|string>,
* created_at: string,
* }&array<string, mixed> $productData
*/
public function __construct(
private array $productData,
private readonly ProductVariantInterface $productVariant,
private readonly ProductInterface $product,
private readonly ChannelInterface $channel,
private readonly string $localeCode,
private readonly array $context,
) {
}

/**
* @return array{
* id: string|int,
* name: string,
* description?: string,
* price: float,
* list_price: float,
* image?: string,
* url: string,
* categories: array<int|string>,
* created_at: string,
* }&array<string, mixed>
*/
public function getProductData(): array
{
return $this->productData;
}

/**
* @param array{
* id: string|int,
* name: string,
* description?: string,
* price: float,
* list_price: float,
* image?: string,
* url: string,
* categories: array<int|string>,
* created_at: string,
* }&array<string, mixed> $productData
*/
public function setProductData(array $productData): void
{
$this->productData = $productData;
}

public function getProductVariant(): ProductVariantInterface
{
return $this->productVariant;
}

public function getProduct(): ProductInterface
{
return $this->product;
}

public function getChannel(): ChannelInterface
{
return $this->channel;
}

public function getLocaleCode(): string
{
return $this->localeCode;
}

public function getContext(): array
{
return $this->context;
}
}
29 changes: 22 additions & 7 deletions src/DataSyncInfrastructure/Normalizer/OrderNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Webgriffe\SyliusClerkPlugin\DataSyncInfrastructure\Normalizer\Event\OrderNormalizerEvent;
Expand All @@ -17,6 +19,7 @@
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private bool $useProductVariants = false,
) {
}

Expand Down Expand Up @@ -112,13 +115,25 @@ private function getProducts(OrderInterface $order): array
*/
private function normalizeOrderItem(OrderItemInterface $item): array
{
$product = $item->getProduct();
if ($product === null) {
throw new \InvalidArgumentException('Order item product cannot be null.');
}
$productId = $product->getId();
if (!is_string($productId) && !is_int($productId)) {
throw new \InvalidArgumentException('Product ID must be a string or an integer, "' . gettype($productId) . '" given.');
if ($this->useProductVariants === true) {
$productVariant = $item->getVariant();
if (!$productVariant instanceof ProductVariantInterface) {
throw new \InvalidArgumentException('Order item product variant cannot be null.');
}
$productId = $productVariant->getId();
if (!is_string($productId) && !is_int($productId)) {
throw new \InvalidArgumentException('Product variant ID must be a string or an integer, "' . gettype($productId) . '" given.');
}
} else {
$product = $item->getProduct();
if (!$product instanceof ProductInterface) {
throw new \InvalidArgumentException('Order item product cannot be null.');
}
/** @var mixed $productId */
$productId = $product->getId();
if (!is_string($productId) && !is_int($productId)) {
throw new \InvalidArgumentException('Product ID must be a string or an integer, "' . gettype($productId) . '" given.');
}
}

return [
Expand Down
Loading

0 comments on commit b6ef99c

Please sign in to comment.