Skip to content

Commit

Permalink
Replace API client (and its mock) in all services configuration (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 11, 2022
1 parent 152c03e commit db2ab99
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/DependencyInjection/WebgriffeSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'arguments' => [
'sylius.factory.product_image',
'sylius.repository.product_image',
'webgriffe_sylius_akeneo.api_client',
'webgriffe_sylius_akeneo.api_client_official',
],
],
'immutable_slug' => [
Expand All @@ -70,7 +70,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'product_option' => [
'class' => ProductOptionValueHandler::class,
'arguments' => [
'webgriffe_sylius_akeneo.api_client',
'webgriffe_sylius_akeneo.api_client_official',
'sylius.repository.product_option',
'sylius.factory.product_option_value',
'sylius.factory.product_option_value_translation',
Expand All @@ -97,7 +97,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'file_attribute' => [
'class' => FileAttributeValueHandler::class,
'arguments' => [
'webgriffe_sylius_akeneo.api_client',
'webgriffe_sylius_akeneo.api_client_official',
'filesystem',
],
],
Expand Down Expand Up @@ -125,7 +125,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'arguments' => [
'$productImageFactory' => 'sylius.factory.product_image',
'$productImageRepository' => 'sylius.repository.product_image',
'$apiClient' => 'webgriffe_sylius_akeneo.api_client',
'$apiClient' => 'webgriffe_sylius_akeneo.api_client_official',
],
],
'immutable_slug' => [
Expand All @@ -140,7 +140,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'product_option' => [
'class' => ProductOptionValueHandler::class,
'arguments' => [
'$apiClient' => 'webgriffe_sylius_akeneo.api_client',
'$apiClient' => 'webgriffe_sylius_akeneo.api_client_official',
'$productOptionRepository' => 'sylius.repository.product_option',
'$productOptionValueFactory' => 'sylius.factory.product_option_value',
'$productOptionValueTranslationFactory' => 'sylius.factory.product_option_value_translation',
Expand Down Expand Up @@ -169,7 +169,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'file_attribute' => [
'class' => FileAttributeValueHandler::class,
'arguments' => [
'$apiClient' => 'webgriffe_sylius_akeneo.api_client',
'$apiClient' => 'webgriffe_sylius_akeneo.api_client_official',
'$filesystem' => 'filesystem',
],
],
Expand Down
29 changes: 11 additions & 18 deletions src/Product/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Webgriffe\SyliusAkeneoPlugin\Product;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Model\ProductInterface;
Expand All @@ -17,7 +20,6 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\FamilyAwareApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webgriffe\SyliusAkeneoPlugin\ReconcilerInterface;
Expand All @@ -34,7 +36,7 @@ public function __construct(
private ProductVariantFactoryInterface $productVariantFactory,
private ProductVariantRepositoryInterface $productVariantRepository,
private ProductRepositoryInterface $productRepository,
private ApiClientInterface $apiClient,
private AkeneoPimClientInterface $apiClient,
private ValueHandlersResolverInterface $valueHandlersResolver,
private ProductFactoryInterface $productFactory,
private TaxonsResolverInterface $taxonsResolver,
Expand Down Expand Up @@ -68,10 +70,7 @@ public function getAkeneoEntity(): string

public function import(string $identifier): void
{
$productVariantResponse = $this->apiClient->findProduct($identifier);
if ($productVariantResponse === null) {
throw new \RuntimeException(sprintf('Cannot find product "%s" on Akeneo.', $identifier));
}
$productVariantResponse = $this->apiClient->getProductApi()->get($identifier);

$product = $this->getOrCreateProductFromVariantResponse($productVariantResponse);

Expand Down Expand Up @@ -118,9 +117,11 @@ public function import(string $identifier): void
* @inheritdoc
* @psalm-return array<array-key, string>
*/
public function getIdentifiersModifiedSince(\DateTime $sinceDate): array
public function getIdentifiersModifiedSince(DateTime $sinceDate): array
{
$products = $this->apiClient->findProductsModifiedSince($sinceDate);
$searchBuilder = new SearchBuilder();
$searchBuilder->addFilter('updated_at', '>', $sinceDate->format('Y-m-d H:i:s'));
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
Assert::string($product['identifier']);
Expand All @@ -136,7 +137,7 @@ public function getIdentifiersModifiedSince(\DateTime $sinceDate): array
*/
public function getAllIdentifiers(): array
{
return $this->getIdentifiersModifiedSince((new \DateTime())->setTimestamp(0));
return $this->getIdentifiersModifiedSince((new DateTime())->setTimestamp(0));
}

private function getOrCreateProductFromVariantResponse(array $productVariantResponse): ProductInterface
Expand Down Expand Up @@ -242,15 +243,7 @@ private function createNewProductFromAkeneoProduct(array $productVariantResponse
*/
private function addMissingAttributes(array $attributesValues, string $familyCode): array
{
if (!$this->apiClient instanceof FamilyAwareApiClientInterface) {
return $attributesValues;
}

$family = $this->apiClient->findFamily($familyCode);

if (null === $family) {
throw new \RuntimeException(sprintf('Cannot find "%s" family on Akeneo.', $familyCode));
}
$family = $this->apiClient->getFamilyApi()->get($familyCode);

/** @var string[] $allFamilyAttributes */
$allFamilyAttributes = $family['attributes'] ?? [];
Expand Down
34 changes: 25 additions & 9 deletions src/ProductAssociations/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@

namespace Webgriffe\SyliusAkeneoPlugin\ProductAssociations;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Exception\HttpException;
use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use RuntimeException;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Product\Model\ProductAssociationInterface;
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Sylius\Component\Product\Model\ProductInterface as BaseProductInterface;
use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webmozart\Assert\Assert;

final class Importer implements ImporterInterface
{
private const AKENEO_ENTITY = 'ProductAssociations';

public function __construct(private ApiClientInterface $apiClient, private ProductRepositoryInterface $productRepository, private RepositoryInterface $productAssociationRepository, private ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository, private FactoryInterface $productAssociationFactory)
{
public function __construct(
private AkeneoPimClientInterface $apiClient,
private ProductRepositoryInterface $productRepository,
private RepositoryInterface $productAssociationRepository,
private ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository,
private FactoryInterface $productAssociationFactory
) {
}

/**
Expand All @@ -38,9 +47,14 @@ public function getAkeneoEntity(): string
*/
public function import(string $identifier): void
{
$productVariantResponse = $this->apiClient->findProduct($identifier);
if ($productVariantResponse === null) {
throw new \RuntimeException(sprintf('Cannot find product "%s" on Akeneo.', $identifier));
try {
$productVariantResponse = $this->apiClient->getProductApi()->get($identifier);
} catch (HttpException $e) {
if ($e->getResponse()->getStatusCode() === 404) {
throw new RuntimeException(sprintf('Cannot find product "%s" on Akeneo.', $identifier));
}

throw $e;
}

$parentCode = $productVariantResponse['parent'];
Expand All @@ -52,7 +66,7 @@ public function import(string $identifier): void

$product = $this->productRepository->findOneByCode($productCode);
if ($product === null) {
throw new \RuntimeException(sprintf('Cannot find product "%s" on Sylius.', $productCode));
throw new RuntimeException(sprintf('Cannot find product "%s" on Sylius.', $productCode));
}
$associations = $productVariantResponse['associations'];
foreach ($associations as $associationTypeCode => $associationInfo) {
Expand Down Expand Up @@ -110,9 +124,11 @@ public function import(string $identifier): void
/**
* @inheritdoc
*/
public function getIdentifiersModifiedSince(\DateTime $sinceDate): array
public function getIdentifiersModifiedSince(DateTime $sinceDate): array
{
$products = $this->apiClient->findProductsModifiedSince($sinceDate);
$searchBuilder = new SearchBuilder();
$searchBuilder->addFilter('updated_at', '>', $sinceDate->format('Y-m-d H:i:s'));
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
$identifiers[] = $product['identifier'];
Expand Down
18 changes: 15 additions & 3 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
<service id="Webgriffe\SyliusAkeneoPlugin\ApiClientInterface" alias="webgriffe_sylius_akeneo.api_client" />
<service id="Webgriffe\SyliusAkeneoPlugin\AttributeOptions\ApiClientInterface" alias="webgriffe_sylius_akeneo.api_client" />

<service id="webgriffe_sylius_akeneo.api_client_official.factory" class="Akeneo\Pim\ApiClient\AkeneoPimClientBuilder">
<argument>%webgriffe_sylius_akeneo.api_client.base_url%</argument>
</service>
<service id="webgriffe_sylius_akeneo.api_client_official" class="Akeneo\Pim\ApiClient\AkeneoPimClient">
<factory service="webgriffe_sylius_akeneo.api_client_official.factory" method="buildAuthenticatedByPassword" />
<argument>%webgriffe_sylius_akeneo.api_client.client_id%</argument>
<argument>%webgriffe_sylius_akeneo.api_client.secret%</argument>
<argument>%webgriffe_sylius_akeneo.api_client.username%</argument>
<argument>%webgriffe_sylius_akeneo.api_client.password%</argument>
</service>
<service id="Akeneo\Pim\ApiClient\AkeneoPimClientInterface" alias="webgriffe_sylius_akeneo.api_client_official" />

<service id="webgriffe_sylius_akeneo.product.value_handlers_resolver" class="Webgriffe\SyliusAkeneoPlugin\PriorityValueHandlersResolver" />
<service id="Webgriffe\SyliusAkeneoPlugin\ValueHandlersResolverInterface" alias="webgriffe_sylius_akeneo.product.value_handlers_resolver" />

Expand Down Expand Up @@ -109,7 +121,7 @@
<service id="Webgriffe\SyliusAkeneoPlugin\Product\TaxonsResolverInterface" alias="webgriffe_sylius_akeneo.product.taxons_resolver" />

<service id="webgriffe_sylius_akeneo.product.product_options_resolver" class="Webgriffe\SyliusAkeneoPlugin\Product\ProductOptionsResolver">
<argument type="service" id="webgriffe_sylius_akeneo.api_client"/>
<argument type="service" id="webgriffe_sylius_akeneo.api_client_official"/>
<argument type="service" id="sylius.repository.product_option" />
<argument type="service" id="sylius.factory.product_option" />
<argument type="service" id="sylius.factory.product_option_translation" />
Expand All @@ -129,7 +141,7 @@
<argument type="service" id="sylius.factory.product_variant" />
<argument type="service" id="sylius.repository.product_variant" />
<argument type="service" id="sylius.repository.product" />
<argument type="service" id="webgriffe_sylius_akeneo.api_client" />
<argument type="service" id="webgriffe_sylius_akeneo.api_client_official" />
<argument type="service" id="webgriffe_sylius_akeneo.product.value_handlers_resolver" />
<argument type="service" id="sylius.factory.product" />
<argument type="service" id="webgriffe_sylius_akeneo.product.taxons_resolver" />
Expand All @@ -145,7 +157,7 @@

<!-- Product Associations Importer -->
<service id="webgriffe_sylius_akeneo.product_associations.importer" class="Webgriffe\SyliusAkeneoPlugin\ProductAssociations\Importer" >
<argument type="service" id="webgriffe_sylius_akeneo.api_client"/>
<argument type="service" id="webgriffe_sylius_akeneo.api_client_official"/>
<argument type="service" id="sylius.repository.product"/>
<argument type="service" id="sylius.repository.product_association"/>
<argument type="service" id="sylius.repository.product_association_type"/>
Expand Down
2 changes: 2 additions & 0 deletions tests/Application/config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
class: 'Tests\Webgriffe\SyliusAkeneoPlugin\Integration\TestDouble\ApiClientMock'
arguments:
- '@webgriffe_sylius_akeneo.temporary_file_manager'
webgriffe_sylius_akeneo.api_client_official:
class: 'Tests\Webgriffe\SyliusAkeneoPlugin\Integration\TestDouble\OfficialApiClientMock'
webgriffe_sylius_akeneo.date_time_builder:
class: 'Tests\Webgriffe\SyliusAkeneoPlugin\Integration\TestDouble\DateTimeBuilder'

Expand Down
54 changes: 54 additions & 0 deletions tests/Integration/TestDouble/FamilyApiMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusAkeneoPlugin\Integration\TestDouble;

use Akeneo\Pim\ApiClient\Api\FamilyApiInterface;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;

final class FamilyApiMock implements FamilyApiInterface
{
public function create(string $code, array $data = []): int
{
// TODO: Implement create() method.
}

public function get(string $code): array
{
return $this->jsonDecodeOrNull(__DIR__ . '/../DataFixtures/ApiClientMock/Family/' . $code . '.json');
}

public function listPerPage(int $limit = 10, bool $withCount = false, array $queryParameters = []): PageInterface
{
// TODO: Implement listPerPage() method.
}

public function all(int $pageSize = 10, array $queryParameters = []): ResourceCursorInterface
{
// TODO: Implement all() method.
}

public function upsert(string $code, array $data = []): int
{
// TODO: Implement upsert() method.
}

public function upsertList($resources): \Traversable
{
// TODO: Implement upsertList() method.
}

/**
* @return mixed|null
*/
private function jsonDecodeOrNull(string $filename)
{
if (file_exists($filename)) {
return json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);
}

return null;
}
}
5 changes: 4 additions & 1 deletion tests/Integration/TestDouble/OfficialApiClientMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ final class OfficialApiClientMock implements AkeneoPimClientInterface

private FamilyVariantApiInterface $familyVariantApi;

private FamilyApiInterface $familyApi;

public function __construct()
{
$this->productApi = new ProductApiMock();
Expand All @@ -64,6 +66,7 @@ public function __construct()
$this->attributeApi = new AttributeApiMock();
$this->productModelApi = new ProductModelApiMock();
$this->familyVariantApi = new FamilyVariantApiMock();
$this->familyApi = new FamilyApiMock();
}

public function addProductUpdatedAt(string $identifier, \DateTime $updatedAt): void
Expand Down Expand Up @@ -108,7 +111,7 @@ public function getAttributeGroupApi(): AttributeGroupApiInterface

public function getFamilyApi(): FamilyApiInterface
{
// TODO: Implement getFamilyApi() method.
return $this->familyApi;
}

public function getProductMediaFileApi(): MediaFileApiInterface
Expand Down

0 comments on commit db2ab99

Please sign in to comment.