deleteNotUsedMedia example

$repo = new StaticEntityRepository(
            [
                [$id1$id2],
                // fake the grace period filter                 fn (Criteria $criteria) => $criteria->getIds(),
                [],
            ],
            $mediaDefinition
        );

        $purger = new UnusedMediaPurger($reponew EventDispatcher());
        $purger->deleteNotUsedMedia();

        static::assertEquals(
            [
                [
                    ['id' => $media1->getId()],
                    ['id' => $media2->getId()],
                ],
            ],
            $repo->deletes
        );
    }

    
/** * @return string[] */
    public static function getHandledMessages(): iterable
    {
        return [CleanupUnusedDownloadMediaTask::class];
    }

    public function run(): void
    {
        $this->unusedMediaPurger->deleteNotUsedMedia(
            null,
            null,
            null,
            ProductDownloadDefinition::ENTITY_NAME
        );
    }
}
return $this->dryRun($input$output);
        }

        $confirm = $io->confirm('Are you sure that you want to delete unused media files?', false);

        if (!$confirm) {
            $io->caution('Aborting due to user input.');

            return self::SUCCESS;
        }

        $count = $this->unusedMediaPurger->deleteNotUsedMedia(
            $input->getOption('limit') ? (int) $input->getOption('limit') : null,
            $input->getOption('offset') !== null ? (int) $input->getOption('offset') : null,
            (int) $input->getOption('grace-period-days'),
            $input->getOption('folder-entity'),
        );

        if ($count === 0) {
            $io->success(sprintf('There are no unused media files uploaded before the grace period of %d days.', (int) $input->getOption('grace-period-days')));

            return self::SUCCESS;
        }

        
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $firstPath = $urlGenerator->getRelativeMediaUrl($txt);
        $secondPath = $urlGenerator->getRelativeMediaUrl($png);
        $thirdPath = $urlGenerator->getRelativeMediaUrl($withProduct);
        $fourthPath = $urlGenerator->getRelativeMediaUrl($withManufacturer);

        $this->getPublicFilesystem()->writeStream($firstPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($secondPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($thirdPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($fourthPathfopen(self::FIXTURE_FILE, 'rb'));

        $this->deleteMediaService->deleteNotUsedMedia($this->context);
        $this->runWorker();

        $result = $this->mediaRepo->search(
            new Criteria([
                $txt->getId(),
                $png->getId(),
                $withProduct->getId(),
                $withManufacturer->getId(),
            ]),
            $this->context
        );

        
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $firstPath = $urlGenerator->getRelativeMediaUrl($txt);
        $secondPath = $urlGenerator->getRelativeMediaUrl($png);
        $thirdPath = $urlGenerator->getRelativeMediaUrl($withProduct);
        $fourthPath = $urlGenerator->getRelativeMediaUrl($withManufacturer);

        $this->getPublicFilesystem()->writeStream($firstPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($secondPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($thirdPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($fourthPath, \fopen(self::FIXTURE_FILE, 'rb'));

        $this->unusedMediaPurger->deleteNotUsedMedia();
        $this->runWorker();

        $result = $this->mediaRepo->search(
            new Criteria([
                $txt->getId(),
                $png->getId(),
                $withProduct->getId(),
                $withManufacturer->getId(),
            ]),
            $this->context
        );

        
Home | Imprint | This part of the site doesn't use cookies.