Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added events before and after processing Taxon and Product entities #54

Merged
merged 5 commits into from Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/CUSTOMIZE.md
Expand Up @@ -11,6 +11,27 @@ These events have two functions :

The Event can modify the Payload which will then be used.

### Before/After processing events

* **Before event** are sent before interracting with the Akeneo resource data.
This allows you to make changes to the received data.
* **After event** are sent after persisting objects and before flushing them.

#### Taxon

* Synolia\SyliusAkeneoPlugin\Event\Category\BeforeProcessingTaxonEvent
* Synolia\SyliusAkeneoPlugin\Event\Category\AfterProcessingTaxonEvent

#### Product

* Synolia\SyliusAkeneoPlugin\Event\Product\BeforeProcessingProductEvent
* Synolia\SyliusAkeneoPlugin\Event\Product\AfterProcessingProductEvent

#### Product Variant

* Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent
* Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent

## Processing Akeneo attribute values

By default, we only use `ProductAttributeAkeneoAttributeProcessor` to insert attribute value to the `ProductAttributeValue` entity, but we provide other processors that you can enable by registering them:
Expand Down
Expand Up @@ -7,7 +7,7 @@
use Sylius\Component\Core\Model\TaxonInterface;
use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class AfterCreateTaxonEvent extends AbstractResourceEvent
class AfterProcessingTaxonEvent extends AbstractResourceEvent
{
/** @var \Sylius\Component\Core\Model\TaxonInterface */
private $taxon;
Expand Down
Expand Up @@ -6,6 +6,6 @@

use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class BeforeCreateTaxonEvent extends AbstractResourceEvent
class BeforeProcessingTaxonEvent extends AbstractResourceEvent
{
}
26 changes: 26 additions & 0 deletions src/Event/Product/AfterProcessingProductEvent.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Event\Product;

use Sylius\Component\Core\Model\ProductInterface;
use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class AfterProcessingProductEvent extends AbstractResourceEvent
{
/** @var \Sylius\Component\Core\Model\ProductInterface */
private $product;

public function __construct(array $resource, ProductInterface $taxon)
{
parent::__construct($resource);

$this->product = $taxon;
}

public function getProduct(): ProductInterface
{
return $this->product;
}
}
11 changes: 11 additions & 0 deletions src/Event/Product/BeforeProcessingProductEvent.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Event\Product;

use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class BeforeProcessingProductEvent extends AbstractResourceEvent
{
}
26 changes: 26 additions & 0 deletions src/Event/ProductVariant/AfterProcessingProductVariantEvent.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Event\ProductVariant;

use Sylius\Component\Core\Model\ProductVariantInterface;
use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class AfterProcessingProductVariantEvent extends AbstractResourceEvent
{
/** @var \Sylius\Component\Core\Model\ProductVariantInterface */
private $productVariant;

public function __construct(array $resource, ProductVariantInterface $taxon)
{
parent::__construct($resource);

$this->productVariant = $taxon;
}

public function getProductVariant(): ProductVariantInterface
{
return $this->productVariant;
}
}
26 changes: 26 additions & 0 deletions src/Event/ProductVariant/BeforeProcessingProductVariantEvent.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Event\ProductVariant;

use Sylius\Component\Core\Model\ProductInterface;
use Synolia\SyliusAkeneoPlugin\Event\AbstractResourceEvent;

class BeforeProcessingProductVariantEvent extends AbstractResourceEvent
{
/** @var \Sylius\Component\Core\Model\ProductInterface */
private $product;

public function __construct(array $resource, ProductInterface $taxon)
{
parent::__construct($resource);

$this->product = $taxon;
}

public function getProduct(): ProductInterface
{
return $this->product;
}
}
8 changes: 4 additions & 4 deletions src/Task/Category/CreateUpdateEntityTask.php
Expand Up @@ -13,8 +13,8 @@
use Sylius\Component\Taxonomy\Factory\TaxonFactoryInterface;
use Sylius\Component\Taxonomy\Model\TaxonTranslationInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Event\Category\AfterCreateTaxonEvent;
use Synolia\SyliusAkeneoPlugin\Event\Category\BeforeCreateTaxonEvent;
use Synolia\SyliusAkeneoPlugin\Event\Category\AfterProcessingTaxonEvent;
use Synolia\SyliusAkeneoPlugin\Event\Category\BeforeProcessingTaxonEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoCategoryResourcesException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
Expand Down Expand Up @@ -86,7 +86,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte

foreach ($payload->getResources() as $resource) {
try {
$this->dispatcher->dispatch(new BeforeCreateTaxonEvent($resource));
$this->dispatcher->dispatch(new BeforeProcessingTaxonEvent($resource));

$this->entityManager->beginTransaction();

Expand Down Expand Up @@ -129,7 +129,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$taxonTranslation->setSlug($slug ?? $resource['code']);
}

$this->dispatcher->dispatch(new AfterCreateTaxonEvent($resource, $taxon));
$this->dispatcher->dispatch(new AfterProcessingTaxonEvent($resource, $taxon));

$this->entityManager->flush();
$this->entityManager->commit();
Expand Down
27 changes: 24 additions & 3 deletions src/Task/Product/CreateConfigurableProductEntitiesTask.php
Expand Up @@ -16,8 +16,11 @@
use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules;
use Synolia\SyliusAkeneoPlugin\Entity\ProductGroup;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoProductFiltersConfigurationException;
use Synolia\SyliusAkeneoPlugin\Exceptions\Processor\MissingAkeneoAttributeProcessorException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
Expand Down Expand Up @@ -77,6 +80,9 @@ final class CreateConfigurableProductEntitiesTask extends AbstractCreateProductE
/** @var \Synolia\SyliusAkeneoPlugin\Provider\AkeneoAttributeProcessorProviderInterface */
private $akeneoAttributeProcessorProvider;

/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
private $dispatcher;

/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -100,7 +106,8 @@ public function __construct(
RepositoryInterface $productVariantTranslationRepository,
FactoryInterface $productVariantTranslationFactory,
ProductFiltersRulesRepository $productFiltersRulesRepository,
AkeneoAttributeProcessorProviderInterface $akeneoAttributeProcessorProvider
AkeneoAttributeProcessorProviderInterface $akeneoAttributeProcessorProvider,
EventDispatcherInterface $dispatcher
) {
parent::__construct(
$entityManager,
Expand All @@ -125,6 +132,7 @@ public function __construct(
$this->productVariantTranslationFactory = $productVariantTranslationFactory;
$this->productFiltersRulesRepository = $productFiltersRulesRepository;
$this->akeneoAttributeProcessorProvider = $akeneoAttributeProcessorProvider;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -170,6 +178,8 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
continue;
}

$this->dispatcher->dispatch(new BeforeProcessingProductVariantEvent($resource, $productModel));

$productGroup = $this->productGroupRepository->findOneBy(['productParent' => $productModel->getCode()]);

if (!$productGroup instanceof ProductGroup) {
Expand All @@ -193,7 +203,9 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
continue;
}

$this->processVariations($payload, $resource['identifier'], $productModel, $resource['values'], $variationAxes);
$productVariant = $this->processVariations($payload, $resource['identifier'], $productModel, $resource['values'], $variationAxes);

$this->dispatcher->dispatch(new AfterProcessingProductVariantEvent($resource, $productVariant));
$this->entityManager->flush();
} catch (\Throwable $throwable) {
$this->logger->warning($throwable->getMessage());
Expand All @@ -217,8 +229,15 @@ private function processVariations(
ProductInterface $productModel,
array $attributes,
array $variationAxes
): void {
): ProductVariantInterface {
$productVariant = $this->getOrCreateEntity($variantCode, $productModel);

/**
* TODO: In the future
* Do not process attributes of the model
* Add family and family_variant to the ProductGroup model for reference
* Foreach attributes not in the last variation axis, remove them.
**/
foreach ($attributes as $attributeCode => $values) {
try {
$processor = $this->akeneoAttributeProcessorProvider->getProcessor($attributeCode, [
Expand Down Expand Up @@ -267,6 +286,8 @@ private function processVariations(
$this->updateImages($payload, $attributes, $productVariant);
$this->setProductPrices($productVariant, $attributes);
}

return $productVariant;
}

private function getLocales(): iterable
Expand Down
19 changes: 18 additions & 1 deletion src/Task/Product/CreateSimpleProductEntitiesTask.php
Expand Up @@ -13,8 +13,13 @@
use Sylius\Component\Product\Generator\SlugGeneratorInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Entity\ProductConfiguration;
use Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules;
use Synolia\SyliusAkeneoPlugin\Event\Product\AfterProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Event\Product\BeforeProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoProductFiltersConfigurationException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
Expand Down Expand Up @@ -73,6 +78,9 @@ final class CreateSimpleProductEntitiesTask extends AbstractCreateProductEntitie
/** @var ProductConfiguration */
private $productConfiguration;

/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
private $dispatcher;

/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -94,7 +102,8 @@ public function __construct(
FactoryInterface $productTranslationFactory,
SlugGeneratorInterface $productSlugGenerator,
SyliusAkeneoLocaleCodeProvider $syliusAkeneoLocaleCodeProvider,
AkeneoAttributeDataProvider $akeneoAttributeDataProvider
AkeneoAttributeDataProvider $akeneoAttributeDataProvider,
EventDispatcherInterface $dispatcher
) {
parent::__construct(
$entityManager,
Expand All @@ -117,6 +126,7 @@ public function __construct(
$this->productSlugGenerator = $productSlugGenerator;
$this->syliusAkeneoLocaleCodeProvider = $syliusAkeneoLocaleCodeProvider;
$this->akeneoAttributeDataProvider = $akeneoAttributeDataProvider;
$this->dispatcher = $dispatcher;
}

public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInterface
Expand Down Expand Up @@ -149,6 +159,8 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$resource = \json_decode($result['values'], true);

try {
$this->dispatcher->dispatch(new BeforeProcessingProductEvent($resource));

$product = $this->getOrCreateEntity($resource);

$this->updateProductRequirementsForActiveLocales(
Expand All @@ -157,6 +169,8 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$resource
);

$this->dispatcher->dispatch(new BeforeProcessingProductVariantEvent($resource, $product));

$productVariant = $this->getOrCreateSimpleVariant($product);
$this->linkCategoriesToProduct($payload, $product, $resource['categories']);

Expand All @@ -168,6 +182,9 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$this->updateImages($payload, $resource, $product);
$this->setProductPrices($productVariant, $resource['values']);

$this->dispatcher->dispatch(new AfterProcessingProductEvent($resource, $product));
$this->dispatcher->dispatch(new AfterProcessingProductVariantEvent($resource, $productVariant));

$this->entityManager->flush();
} catch (\Throwable $throwable) {
$this->logger->warning($throwable->getMessage());
Expand Down
20 changes: 13 additions & 7 deletions src/Task/ProductModel/AddOrUpdateProductModelTask.php
Expand Up @@ -4,7 +4,6 @@

namespace Synolia\SyliusAkeneoPlugin\Task\ProductModel;

use App\Entity\Product\Product;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -19,9 +18,12 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Entity\ProductConfiguration;
use Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules;
use Synolia\SyliusAkeneoPlugin\Entity\ProductGroup;
use Synolia\SyliusAkeneoPlugin\Event\Product\AfterProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Event\Product\BeforeProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoProductFiltersConfigurationException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
Expand Down Expand Up @@ -126,6 +128,9 @@ final class AddOrUpdateProductModelTask implements AkeneoTaskInterface
/** @var \Synolia\SyliusAkeneoPlugin\Task\AkeneoTaskInterface */
private $addProductCategoriesTask;

/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
private $dispatcher;

/**
* @param \Synolia\SyliusAkeneoPlugin\Repository\ProductGroupRepository $productGroupRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -147,7 +152,8 @@ public function __construct(
EntityRepository $productConfigurationRepository,
FactoryInterface $productTranslationFactory,
SlugGeneratorInterface $productSlugGenerator,
AkeneoFamilyPropertiesProvider $akeneoFamilyPropertiesProvider
AkeneoFamilyPropertiesProvider $akeneoFamilyPropertiesProvider,
EventDispatcherInterface $dispatcher
) {
$this->entityManager = $entityManager;
$this->productFactory = $productFactory;
Expand All @@ -166,6 +172,7 @@ public function __construct(
$this->productTranslationFactory = $productTranslationFactory;
$this->productSlugGenerator = $productSlugGenerator;
$this->akeneoFamilyPropertiesProvider = $akeneoFamilyPropertiesProvider;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -199,9 +206,13 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$resource = \json_decode($result['values'], true);

try {
$this->dispatcher->dispatch(new BeforeProcessingProductEvent($resource));

$this->entityManager->beginTransaction();
$product = $this->process($resource);

$this->dispatcher->dispatch(new AfterProcessingProductEvent($resource, $product));

$this->entityManager->flush();
$this->entityManager->commit();
$this->entityManager->clear();
Expand Down Expand Up @@ -281,11 +292,6 @@ private function process(array &$resource): ProductInterface
return $product;
}

/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*
* @todo Need refacto
*/
private function addOrUpdate(ProductInterface $product, array &$resource): void
{
if (!isset($resource['family'])) {
Expand Down