diff --git a/.github/workflows/static-tests.yml b/.github/workflows/static-tests.yml new file mode 100644 index 0000000..248d306 --- /dev/null +++ b/.github/workflows/static-tests.yml @@ -0,0 +1,10 @@ +name: Static Tests +on: [push] + +jobs: + coding-standards: + name: Coding Standards + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: extdn/github-actions-m2/magento-coding-standard/8.1@master diff --git a/Console/Command/RemoveMediaDuplicates.php b/Console/Command/RemoveMediaDuplicates.php index 308ff5f..10a0ad6 100644 --- a/Console/Command/RemoveMediaDuplicates.php +++ b/Console/Command/RemoveMediaDuplicates.php @@ -12,10 +12,16 @@ use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RemoveMediaDuplicates extends Command { + /** + * List of available options + */ + private const OPTION_DRY_RUN = 'dry-run'; + /** * @param OriginFinder $originFinder * @param ImageBuilder $imageBuilder @@ -44,6 +50,13 @@ protected function configure() ->setName('catalog:images:remove-duplicates') ->setDescription('Searches for media duplicates in database and removes them'); + $this->addOption( + self::OPTION_DRY_RUN, + null, + InputOption::VALUE_NONE, + 'Performs dry running without any operations with database' + ); + parent::configure(); } @@ -68,9 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $duplicateImage = $this->imageBuilder->create($image->getValue()); $originImage = $this->originFinder->getOriginImage($duplicateImage); - $this->imageResource->save( - $image->setValue($originImage->getCatalogPath()) - ); + if (!$this->isDryRun($input)) { + $this->imageResource->save( + $image->setValue($originImage->getCatalogPath()) + ); + } $removedImages[] = $duplicateImage; $totalSize += $duplicateImage->getFileSize(); @@ -79,6 +94,12 @@ protected function execute(InputInterface $input, OutputInterface $output) } } + if ($this->isDryRun($input)) { + $output->writeln( + 'Dry mode enabled, no database operations performed' + ); + } + $output->writeln( sprintf( 'Number of images successfully removed: %d with size of %.2f MB', @@ -89,4 +110,15 @@ protected function execute(InputInterface $input, OutputInterface $output) return Cli::RETURN_SUCCESS; } + + /** + * If the command executes in dry run mode + * + * @param InputInterface $input + * @return bool + */ + private function isDryRun(InputInterface $input): bool + { + return $input->getOption(self::OPTION_DRY_RUN); + } } diff --git a/Model/Gallery/Image.php b/Model/Gallery/Image.php index 932113b..ab009ad 100644 --- a/Model/Gallery/Image.php +++ b/Model/Gallery/Image.php @@ -60,6 +60,7 @@ public function getCatalogPath(): string */ public function getPathInfo(): PathInfo { + // phpcs:ignore $pathInfo = pathinfo($this->getAbsolutePath()); return $this->pathInfoFactory->create() @@ -76,6 +77,7 @@ public function getFileSize(): int { $absolutePath = $this->getAbsolutePath(); + // phpcs:ignore $fileSize = @filesize($absolutePath); if (!$fileSize) { throw new FileSystemException( @@ -98,6 +100,7 @@ public function getHash(): string $hash = $this->hashCache->get($absolutePath); if (!$hash) { + // phpcs:ignore $hash = @hash_file(self::HASH_ALGORITHM, $absolutePath) ?: null; $this->hashCache->set($absolutePath, $hash); } diff --git a/Model/Gallery/Image/HashCache.php b/Model/Gallery/Image/HashCache.php index 9b703ef..bba3516 100644 --- a/Model/Gallery/Image/HashCache.php +++ b/Model/Gallery/Image/HashCache.php @@ -24,6 +24,8 @@ public function get(string $path): ?string /** * Set file hash by path * + * @param string $path + * @param string|null $hash * @return $this */ public function set(string $path, ?string $hash): self diff --git a/README.md b/README.md index 573e189..00585e9 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ original path. That's easy - `bin/magento catalog:images:remove-duplicates` +Possible options: + +- `--dry-run` - performs dry running without any operations with database + **Warning** Please create dumps of below listed tables, for being able to revert changes if something goes wrong.