Skip to content

Commit

Permalink
Remove only temporary files from current akeneo entity import (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Oct 9, 2023
1 parent f78efc3 commit 1ef53af
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 42 deletions.
16 changes: 16 additions & 0 deletions docs/upgrade/upgrade-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ Removed all deprecations of the v1.x releases.
- [BC] Class Webgriffe\SyliusAkeneoPlugin\Repository\QueueItemRepositoryInterface has been deleted
- [BC] Class Webgriffe\SyliusAkeneoPlugin\Entity\QueueItemInterface has been deleted

### Remove only temporary files from current akeneo entity import ([#176](https://github.com/webgriffe/SyliusAkeneoPlugin/pull/176))

#### TL;DR

Now, the Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManagerInterface service requires a file identifier to remove only
the temporary files related to the current Akeneo entity import.

#### BC Breaks

##### Changed
- [BC] The number of required arguments for Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManager#generateTemporaryFilePath() increased from 0 to 1
- [BC] The number of required arguments for Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManager#deleteAllTemporaryFiles() increased from 0 to 1
- [BC] The number of required arguments for Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManagerInterface#generateTemporaryFilePath() increased from 0 to 1
- [BC] The number of required arguments for Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManagerInterface#deleteAllTemporaryFiles() increased from 0 to 1


### Test changes

#### TL;DR
Expand Down
3 changes: 2 additions & 1 deletion spec/ValueHandler/FileAttributeValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public function let(
$supportChannel->setCode('support');
$product->getChannels()->willReturn(new ArrayCollection([$commerceChannel, $supportChannel]));
$productVariant->getProduct()->willReturn($product);
$productVariant->getCode()->willReturn('VARIANT_1');
$apiClient->getAttributeApi()->willReturn($attributeApi);
$apiClient->getProductMediaFileApi()->willReturn($productMediaFileApi);
$productMediaFileApi->download(Argument::type('string'))->willReturn(new Response(200, [], '__FILE_CONTENT__'));
$attributeApi->get('allegato_1')->willReturn(['type' => 'pim_catalog_file']);
$temporaryFilesManager->generateTemporaryFilePath()->willReturn('tempfile');
$temporaryFilesManager->generateTemporaryFilePath('product-variant-VARIANT_1')->willReturn('tempfile');
$this->beConstructedWith($apiClient, $filesystem, $temporaryFilesManager, 'allegato_1', 'public/media/attachment/product/');
}

Expand Down
3 changes: 2 additions & 1 deletion spec/ValueHandler/ImageValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ public function let(
$product->getChannels()->willReturn(new ArrayCollection([$commerceChannel, $supportChannel]));
$productVariant->addImage($productImage)->hasReturnVoid();
$productVariant->getProduct()->willReturn($product);
$productVariant->getCode()->willReturn('VARIANT_1');
$productImageRepository
->findBy(['owner' => $product, 'type' => self::SYLIUS_IMAGE_TYPE])
->willReturn(new ArrayCollection([]));
$temporaryFilesManager->generateTemporaryFilePath()->willReturn('tempfile');
$temporaryFilesManager->generateTemporaryFilePath('product-variant-VARIANT_1')->willReturn('tempfile');
$this->beConstructedWith(
$productImageFactory,
$productImageRepository,
Expand Down
3 changes: 2 additions & 1 deletion src/MessageHandler/ItemImportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Webgriffe\SyliusAkeneoPlugin\ImporterRegistryInterface;
use Webgriffe\SyliusAkeneoPlugin\Message\ItemImport;
use Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManager;
use Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManagerInterface;

final class ItemImportHandler
{
Expand All @@ -26,7 +27,7 @@ public function __invoke(ItemImport $message): void
try {
$importer->import($akeneoIdentifier);
} finally {
$this->temporaryFilesManager->deleteAllTemporaryFiles();
$this->temporaryFilesManager->deleteAllTemporaryFiles(TemporaryFilesManagerInterface::PRODUCT_VARIANT_PREFIX . $akeneoIdentifier);
}
}

Expand Down
28 changes: 22 additions & 6 deletions src/TemporaryFilesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@

final class TemporaryFilesManager implements TemporaryFilesManagerInterface
{
public function __construct(private Filesystem $filesystem, private Finder $finder, private string $temporaryDirectory, private string $temporaryFilesPrefix)
{
public function __construct(
private Filesystem $filesystem,
private Finder $finder,
private string $temporaryDirectory,
private string $temporaryFilesPrefix,
) {
}

public function generateTemporaryFilePath(): string
public function generateTemporaryFilePath(string $fileIdentifier): string
{
return $this->filesystem->tempnam($this->temporaryDirectory, $this->temporaryFilesPrefix);
return $this->filesystem->tempnam(
$this->temporaryDirectory,
$this->getFilePrefix($fileIdentifier),
);
}

public function deleteAllTemporaryFiles(): void
public function deleteAllTemporaryFiles(string $fileIdentifier): void
{
$tempFiles = $this->finder->in($this->temporaryDirectory)->depth('== 0')->files()->name(
$this->temporaryFilesPrefix . '*',
$this->getFilePrefix($fileIdentifier) . '*',
);
foreach ($tempFiles as $tempFile) {
$this->filesystem->remove($tempFile->getPathname());
}
}

private function getFilePrefix(string $fileIdentifier): string
{
return sprintf(
'%s-%s-',
rtrim($this->temporaryFilesPrefix, '-'),
rtrim($fileIdentifier, '-'),
);
}
}
6 changes: 4 additions & 2 deletions src/TemporaryFilesManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

interface TemporaryFilesManagerInterface
{
public function generateTemporaryFilePath(): string;
public const PRODUCT_VARIANT_PREFIX = 'product-variant-';

public function deleteAllTemporaryFiles(): void;
public function generateTemporaryFilePath(string $fileIdentifier): string;

public function deleteAllTemporaryFiles(string $fileIdentifier): void;
}
13 changes: 5 additions & 8 deletions src/ValueHandler/FileAttributeValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function handle($subject, string $attribute, array $value): void
// TODO remove existing image? See https://github.com/webgriffe/SyliusAkeneoPlugin/issues/61
return;
}
$downloadedFile = $this->downloadFile($mediaCode);
$fileIdentifier = $subject->getCode();
Assert::notNull($fileIdentifier);
$downloadedFile = $this->downloadFile($mediaCode, $fileIdentifier);

$relativeFilePath = $mediaCode;
$this->moveFileToAttachmentFolder($relativeFilePath, $downloadedFile);
Expand Down Expand Up @@ -133,7 +135,7 @@ private function getValue(array $value, ProductInterface $product): ?string
throw new InvalidArgumentException('Invalid Akeneo attachment data: cannot find the media code.');
}

private function downloadFile(string $mediaCode): SplFileInfo
private function downloadFile(string $mediaCode, string $fileIdentifier): SplFileInfo
{
$response = $this->apiClient->getProductMediaFileApi()->download($mediaCode);
$statusClass = (int) ($response->getStatusCode() / 100);
Expand All @@ -144,14 +146,9 @@ private function downloadFile(string $mediaCode): SplFileInfo

throw new SymfonyHttpException((int) $responseResult['code'], (string) $responseResult['message']);
}
$tempName = $this->generateTempFilePath();
$tempName = $this->temporaryFilesManager->generateTemporaryFilePath(TemporaryFilesManagerInterface::PRODUCT_VARIANT_PREFIX . $fileIdentifier);
file_put_contents($tempName, $bodyContents);

return new File($tempName);
}

private function generateTempFilePath(): string
{
return $this->temporaryFilesManager->generateTemporaryFilePath();
}
}
13 changes: 5 additions & 8 deletions src/ValueHandler/ImageValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function handle($subject, string $attribute, array $value): void

return;
}
$imageFile = $this->downloadFile($mediaCode);
$fileIdentifier = $subject->getCode();
Assert::notNull($fileIdentifier);
$imageFile = $this->downloadFile($mediaCode, $fileIdentifier);

$productImage = $this->getExistentProductVariantImage($subject, $product);
if ($productImage === null) {
Expand Down Expand Up @@ -168,7 +170,7 @@ private function getValue(array $value, ProductInterface $product): ?string
throw new InvalidArgumentException('Invalid Akeneo value data: cannot find the media code.');
}

private function downloadFile(string $mediaCode): SplFileInfo
private function downloadFile(string $mediaCode, string $fileIdentifier): SplFileInfo
{
$response = $this->apiClient->getProductMediaFileApi()->download($mediaCode);
$statusClass = (int) ($response->getStatusCode() / 100);
Expand All @@ -179,14 +181,9 @@ private function downloadFile(string $mediaCode): SplFileInfo

throw new HttpException((int) $responseResult['code'], (string) $responseResult['message']);
}
$tempName = $this->generateTempFilePath();
$tempName = $this->temporaryFilesManager->generateTemporaryFilePath(TemporaryFilesManagerInterface::PRODUCT_VARIANT_PREFIX . $fileIdentifier);
file_put_contents($tempName, $bodyContents);

return new File($tempName);
}

private function generateTempFilePath(): string
{
return $this->temporaryFilesManager->generateTemporaryFilePath();
}
}
8 changes: 8 additions & 0 deletions tests/Behat/Context/Cli/ImportCommandContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function __construct(
) {
}

/**
* @BeforeScenario
*/
public function before(): void
{
vfsStream::setup();
}

/**
* @When I import items for all importers modified since date :date
*/
Expand Down
9 changes: 3 additions & 6 deletions tests/Behat/Context/System/FilesystemContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
namespace Tests\Webgriffe\SyliusAkeneoPlugin\Behat\Context\System;

use Behat\Behat\Context\Context;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamContainer;
use Webmozart\Assert\Assert;

final class FilesystemContext implements Context
{
/** @phpstan-ignore-next-line */
private ?vfsStreamContainer $vfsStream = null;

public function __construct(private string $temporaryDirectory, private string $temporaryFilesPrefix)
{
}
Expand All @@ -23,7 +18,9 @@ public function __construct(private string $temporaryDirectory, private string $
*/
public function before(): void
{
$this->vfsStream = vfsStream::setup('root');
if (!file_exists($this->temporaryDirectory)) {
mkdir($this->temporaryDirectory);
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="webgriffe_sylius_akeneo.temporary_directory">%kernel.project_dir%/var/tmp/</parameter>
</parameters>
<services>
<defaults public="true" />

Expand Down
20 changes: 11 additions & 9 deletions tests/Unit/TemporaryFilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,33 @@ public function it_generates_temporary_file_path(): void
{
$this->assertMatchesRegularExpression(
'|' . vfsStream::url('root') . '/akeneo-.*|',
$this->temporaryFileManager->generateTemporaryFilePath(),
$this->temporaryFileManager->generateTemporaryFilePath('VARIANT_1'),
);
}

/** @test */
public function it_deletes_all_temporary_files(): void
{
touch(vfsStream::url('root') . '/akeneo-temp1');
touch(vfsStream::url('root') . '/akeneo-temp2');
touch(vfsStream::url('root') . '/akeneo-temp3');
touch(vfsStream::url('root') . '/akeneo-VARIANT_1-temp1');
touch(vfsStream::url('root') . '/akeneo-VARIANT_1-temp2');
touch(vfsStream::url('root') . '/akeneo-VARIANT_1-temp3');

$this->temporaryFileManager->deleteAllTemporaryFiles();
$this->temporaryFileManager->deleteAllTemporaryFiles('VARIANT_1');

$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-temp1');
$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-temp2');
$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-temp3');
$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-VARIANT_1-temp1');
$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-VARIANT_1-temp2');
$this->assertFileDoesNotExist(vfsStream::url('root') . '/akeneo-VARIANT_1-temp3');
}

/** @test */
public function it_does_not_delete_not_managed_temporary_files(): void
{
touch(vfsStream::url('root') . '/not-managed-temp-file');
touch(vfsStream::url('root') . '/VARIANT_1-not-managed-temp-file');

$this->temporaryFileManager->deleteAllTemporaryFiles();
$this->temporaryFileManager->deleteAllTemporaryFiles('VARIANT_1');

$this->assertFileExists(vfsStream::url('root') . '/not-managed-temp-file');
$this->assertFileExists(vfsStream::url('root') . '/VARIANT_1-not-managed-temp-file');
}
}

0 comments on commit 1ef53af

Please sign in to comment.