-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to delete excluded paths afterward
- Loading branch information
Showing
5 changed files
with
152 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tinect\Redirects\Command; | ||
|
||
use Shopware\Core\Framework\Api\Context\SystemSource; | ||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Tinect\Redirects\Content\Redirect\RedirectEntity; | ||
use Tinect\Redirects\Services\ExcludedService; | ||
|
||
#[AsCommand(name: 'tinect-redirects:excludes-cleanup', description: 'Cleanup existing entries by re-checking them for the exclude patterns.')] | ||
class CleanupExcludesCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly EntityRepository $tinectRedirectsRedirectRepository, | ||
private readonly ExcludedService $excludedService | ||
) { | ||
parent::__construct($this->getName()); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addOption('dry-run', 'd', null, 'Dry run will not delete any entries but prints ids.'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('Collecting entries:'); | ||
|
||
$context = new Context(new SystemSource()); | ||
$criteria = (new Criteria()) | ||
->setLimit(500) | ||
->addFilter(new EqualsFilter('active', false)) | ||
->addAssociation('salesChannelDomain'); | ||
|
||
$repositoryIterator = new RepositoryIterator($this->tinectRedirectsRedirectRepository, $context, $criteria); | ||
|
||
$progressBar = new ProgressBar($output, $repositoryIterator->getTotal()); | ||
$progressBar->start(); | ||
|
||
$deleteIds = []; | ||
while (($result = $repositoryIterator->fetch()) !== null) { | ||
foreach ($result->getEntities() as $redirect) { | ||
$progressBar->advance(); | ||
|
||
if (!$redirect instanceof RedirectEntity) { | ||
continue; | ||
} | ||
|
||
$salesChannelId = $redirect->getSalesChannelDomain()?->getSalesChannelId(); | ||
|
||
if ($this->excludedService->isExcluded($redirect->getSource(), $salesChannelId)) { | ||
$deleteIds[] = $redirect->getId(); | ||
} | ||
} | ||
} | ||
|
||
$progressBar->finish(); | ||
$output->writeln(''); | ||
|
||
$countDeletes = \count($deleteIds); | ||
|
||
if ($countDeletes === 0) { | ||
$output->writeln('No entries to delete.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
if ($input->getOption('dry-run')) { | ||
$output->writeln('Would delete ' . $countDeletes . ' entries'); | ||
} else { | ||
$output->writeln('Deleting ' . $countDeletes . ' entries:'); | ||
|
||
$progressBar = new ProgressBar($output, $countDeletes); | ||
$progressBar->start(); | ||
|
||
$deleteIds = \array_chunk($deleteIds, 1000); | ||
foreach ($deleteIds as $ids) { | ||
$this->tinectRedirectsRedirectRepository->delete(\array_map(static fn ($id) => ['id' => $id], $ids), $context); | ||
$progressBar->advance(\count($ids)); | ||
} | ||
|
||
$progressBar->finish(); | ||
$output->writeln(''); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tinect\Redirects\Services; | ||
|
||
use Shopware\Core\System\SystemConfig\SystemConfigService; | ||
|
||
class ExcludedService | ||
{ | ||
/** | ||
* Key is sales channel id, value is an array of exclude patterns. | ||
* | ||
* @var array<string, array<string>> | ||
*/ | ||
private array $excludes = []; | ||
|
||
public function __construct( | ||
private readonly SystemConfigService $systemConfigService, | ||
) { | ||
} | ||
|
||
public function isExcluded(string $path, ?string $salesChannelId): bool | ||
{ | ||
if (!isset($this->excludes[$salesChannelId])) { | ||
$excludes = \array_filter(\explode(PHP_EOL, $this->systemConfigService->getString('TinectRedirects.config.excludes', $salesChannelId))); | ||
$this->excludes[$salesChannelId] = $excludes; | ||
} | ||
|
||
$excludes = $this->excludes[$salesChannelId]; | ||
|
||
foreach ($excludes as $exclude) { | ||
try { | ||
if (\preg_match($exclude, $path)) { | ||
return true; | ||
} | ||
} catch (\Throwable) { | ||
// nth, we don't care whether the regex is valid | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters