diff --git a/composer.json b/composer.json index bd725790..37b750a4 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,11 @@ "http-interop/http-factory-guzzle": "^1.0", "league/pipeline": "^1.0", "php-http/guzzle6-adapter": "^2.0", + "php-http/message-factory": "^1.1", "psr/event-dispatcher": "^1.0", "sylius/sylius": "^1.10", "symfony/framework-bundle": "^5.4|^6.0", + "symfony/http-client": "^5.4|^6.0", "symfony/lock": "^5.4|^6.0", "symfony/property-access": "^4.3|^5.4|^6.0", "symfony/property-info": "^5.4|^6.0", diff --git a/src/Client/ClientFactory.php b/src/Client/ClientFactory.php index 469e4b1c..0c767670 100644 --- a/src/Client/ClientFactory.php +++ b/src/Client/ClientFactory.php @@ -6,6 +6,10 @@ use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder; use Akeneo\Pim\ApiClient\AkeneoPimClientInterface; +use Symfony\Component\HttpClient\CachingHttpClient; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpClient\HttplugClient; +use Symfony\Component\HttpKernel\HttpCache\Store; use Synolia\SyliusAkeneoPlugin\Entity\ApiConfiguration; use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\ApiConnectionProviderInterface; @@ -13,8 +17,10 @@ final class ClientFactory implements ClientFactoryInterface { private ?AkeneoPimClientInterface $akeneoClient = null; - public function __construct(private ApiConnectionProviderInterface $apiConnectionProvider) - { + public function __construct( + private ApiConnectionProviderInterface $apiConnectionProvider, + private string $cacheDir, + ) { } public function createFromApiCredentials(): AkeneoPimClientInterface @@ -27,6 +33,19 @@ public function createFromApiCredentials(): AkeneoPimClientInterface $client = new AkeneoPimClientBuilder($apiConnection->getBaseUrl()); + $httpClient = HttpClient::create(); + + $path = $this->cacheDir . '/akeneo'; + + if (is_dir($path) === false) { + mkdir($path); + } + + $store = new Store($path); + $httpClient = new CachingHttpClient($httpClient, $store, ['default_ttl' => 3600, 'allow_revalidate' => true, 'debug' => getenv('APP_DEBUG')]); + + $client->setHttpClient(new HttplugClient($httpClient)); + $this->akeneoClient = $client->buildAuthenticatedByPassword( $apiConnection->getApiClientId(), $apiConnection->getApiClientSecret(), diff --git a/src/Processor/AbstractImageProcessor.php b/src/Processor/AbstractImageProcessor.php index de0a1cd7..5b58c0a4 100644 --- a/src/Processor/AbstractImageProcessor.php +++ b/src/Processor/AbstractImageProcessor.php @@ -14,7 +14,7 @@ use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; -use Synolia\SyliusAkeneoPlugin\Client\ClientFactory; +use Synolia\SyliusAkeneoPlugin\Client\ClientFactoryInterface; use Synolia\SyliusAkeneoPlugin\Entity\ProductConfiguration; use Synolia\SyliusAkeneoPlugin\Entity\ProductConfigurationImageMapping; use Throwable; @@ -29,7 +29,7 @@ public function __construct( private EntityManagerInterface $entityManager, private FactoryInterface $productImageFactory, protected LoggerInterface $logger, - private ClientFactory $clientFactory, + private ClientFactoryInterface $clientFactory, ) { } diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 0c16393f..7358673f 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -5,6 +5,7 @@ services: public: false bind: $projectDir: '%kernel.project_dir%' + $cacheDir: '%kernel.cache_dir%' Synolia\SyliusAkeneoPlugin\: resource: '../../*'