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

Ability to customize which identifiers to enqueue #172

Merged
merged 2 commits into from
Sep 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@
<argument type="service" id="sylius.repository.product_association"/>
<argument type="service" id="sylius.repository.product_association_type"/>
<argument type="service" id="sylius.factory.product_association"/>
<argument type="service" id="event_dispatcher" />
<tag name="webgriffe_sylius_akeneo.importer" />
</service>

<!-- Attribute Options Importer -->
<service id="webgriffe_sylius_akeneo.attribute_options.importer" class="Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer">
<argument type="service" id="webgriffe_sylius_akeneo.api_client"/>
<argument type="service" id="sylius.repository.product_attribute"/>
<argument type="service" id="event_dispatcher" />
<tag name="webgriffe_sylius_akeneo.importer" />
</service>

Expand Down
43 changes: 43 additions & 0 deletions docs/architecture_and_customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,46 @@ Another provided importer is the **attribute options
importer** (`\Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer`). This importer imports the Akeneo simple select
and multi select attributes options into Sylius select attributes. The select attributes must already exist on Sylius
with the same code they have on Akeneo.

## Customize which Akeneo products to import

Each built-in importer described above implements a `getIdentiferModifiedSince()` method.
This method is used to identify which Akeneo entity identifiers should be imported or reconciled when the corresponding CLI commands run.
By default, those methods return all the Akeneo identifiers of entities modified since the given date.
But maybe you want to only import a subset of those Akeneo entities.

For example, you might want to only import Akeneo products that have a not-empty family and a completeness of 100% for the `ecommerce` Akeneo channel.
To do so you can define an event listener or subscriber for the `Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent` event and add the corresponding filters to the `SearchBuilder` object:

```php
// src/EventSubscriber/IdentifiersModifiedSinceSearchBuilderBuiltEventSubscriber.php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\Product\Importer as ProductImporter;
use Webgriffe\SyliusAkeneoPlugin\ProductAssociations\Importer as ProductAssociationsImporter;

final class IdentifiersModifiedSinceSearchBuilderBuiltEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
IdentifiersModifiedSinceSearchBuilderBuiltEvent::class => 'onIdentifiersModifiedSinceSearchBuilderBuilt',
];
}

public function onIdentifiersModifiedSinceSearchBuilderBuilt(IdentifiersModifiedSinceSearchBuilderBuiltEvent $event): void
{
if (!$event->getImporter() instanceof ProductImporter &&
!$event->getImporter() instanceof ProductAssociationsImporter) {
return;
}

$searchBuilder = $event->getSearchBuilder();
$searchBuilder->addFilter('family', 'NOT EMPTY');
$searchBuilder->addFilter('completeness', '=', 100, ['scope' => 'ecommerce']);
}
}
```
15 changes: 12 additions & 3 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Webgriffe\SyliusAkeneoPlugin\AttributeOptions;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;

final class Importer implements ImporterInterface
Expand All @@ -22,6 +25,7 @@ final class Importer implements ImporterInterface
public function __construct(
private AkeneoPimClientInterface $apiClient,
private RepositoryInterface $attributeRepository,
private EventDispatcherInterface $eventDispatcher,
) {
}

Expand Down Expand Up @@ -59,13 +63,18 @@ public function import(string $identifier): void
}

/**
* It's not possible to fetch only attributes or attribute options modified since a given date with the Akeneo
* REST API. So, the $sinceDate argument it's not used.
* As stated at https://api.akeneo.com/documentation/filter.html#by-update-date-3:
* > For Simple select and Multiple select attribute, an option update isn't considered as an attribute update.
* So, the $sinceDate argument it's not used here.
*/
public function getIdentifiersModifiedSince(\DateTime $sinceDate): array
{
$searchBuilder = new SearchBuilder();
$this->eventDispatcher->dispatch(
new IdentifiersModifiedSinceSearchBuilderBuiltEvent($this, $searchBuilder, $sinceDate),
);
/** @var array<array-key, array<string, mixed>> $akeneoAttributes */
$akeneoAttributes = $this->apiClient->getAttributeApi()->all();
$akeneoAttributes = $this->apiClient->getAttributeApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$syliusSelectAttributes = $this->attributeRepository->findBy(['type' => SelectAttributeType::TYPE]);
$syliusSelectAttributes = array_filter(
array_map(
Expand Down
37 changes: 37 additions & 0 deletions src/Event/IdentifiersModifiedSinceSearchBuilderBuiltEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusAkeneoPlugin\Event;

use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use DateTimeInterface;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;

/**
* @readonly
*/
final class IdentifiersModifiedSinceSearchBuilderBuiltEvent
{
public function __construct(
private ImporterInterface $importer,
private SearchBuilder $searchBuilder,
private DateTimeInterface $sinceDate,
) {
}

public function getImporter(): ImporterInterface
{
return $this->importer;
}

public function getSearchBuilder(): SearchBuilder
{
return $this->searchBuilder;
}

public function getSinceDate(): DateTimeInterface
{
return $this->sinceDate;
}
}
4 changes: 4 additions & 0 deletions src/Product/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webgriffe\SyliusAkeneoPlugin\ReconcilerInterface;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlersResolverInterface;
Expand Down Expand Up @@ -112,6 +113,9 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
{
$searchBuilder = new SearchBuilder();
$searchBuilder->addFilter('updated', '>', $sinceDate->format('Y-m-d H:i:s'));
$this->eventDispatcher->dispatch(
new IdentifiersModifiedSinceSearchBuilderBuiltEvent($this, $searchBuilder, $sinceDate),
);
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
Expand Down
6 changes: 6 additions & 0 deletions src/ProductAssociations/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webmozart\Assert\Assert;

Expand All @@ -35,6 +37,7 @@ public function __construct(
private RepositoryInterface $productAssociationRepository,
private ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository,
private FactoryInterface $productAssociationFactory,
private EventDispatcherInterface $eventDispatcher,
) {
}

Expand Down Expand Up @@ -130,6 +133,9 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
{
$searchBuilder = new SearchBuilder();
$searchBuilder->addFilter('updated', '>', $sinceDate->format('Y-m-d H:i:s'));
$this->eventDispatcher->dispatch(
new IdentifiersModifiedSinceSearchBuilderBuiltEvent($this, $searchBuilder, $sinceDate),
);
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
Expand Down
Loading