Skip to content

Commit 3feceee

Browse files
committed
Make prune command more user friendly
1 parent eb54f69 commit 3feceee

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/Command/PruneCommand.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Setono\SyliusConversionAttributionPlugin\Command;
66

7+
use Doctrine\ORM\QueryBuilder;
78
use Doctrine\Persistence\ManagerRegistry;
89
use Setono\Doctrine\ORMTrait;
910
use Setono\SyliusConversionAttributionPlugin\Model\SourceInterface;
1011
use Symfony\Component\Console\Attribute\AsCommand;
1112
use Symfony\Component\Console\Command\Command;
1213
use Symfony\Component\Console\Input\InputInterface;
1314
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Console\Style\SymfonyStyle;
1416
use Webmozart\Assert\Assert;
1517

1618
#[AsCommand(
@@ -36,8 +38,20 @@ public function __construct(
3638

3739
protected function execute(InputInterface $input, OutputInterface $output): int
3840
{
39-
while (($deletableIds = $this->provideDeletableIds()) !== []) {
40-
$this
41+
$io = new SymfonyStyle($input, $output);
42+
$io->title('Pruning old conversion attribution sources');
43+
44+
$total = (int) $this->getDeletableQueryBuilder()->select('COUNT(o)')->getQuery()->getSingleScalarResult();
45+
if ($total === 0) {
46+
$io->success('No sources to prune');
47+
48+
return 0;
49+
}
50+
51+
$io->progressStart($total);
52+
53+
while (($deletableIds = $this->getDeletableIds()) !== []) {
54+
$progress = (int) $this
4155
->getRepository($this->sourceClass)
4256
->createQueryBuilder('o')
4357
->delete()
@@ -46,22 +60,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4660
->getQuery()
4761
->execute()
4862
;
63+
64+
$io->progressAdvance($progress);
4965
}
5066

67+
$io->progressFinish();
68+
5169
return 0;
5270
}
5371

54-
private function provideDeletableIds(): array
72+
private function getDeletableIds(): array
73+
{
74+
return $this
75+
->getDeletableQueryBuilder()
76+
->select('o.id')
77+
->setMaxResults(1)
78+
->getQuery()
79+
->getScalarResult()
80+
;
81+
}
82+
83+
private function getDeletableQueryBuilder(): QueryBuilder
5584
{
5685
return $this
5786
->getRepository($this->sourceClass)
5887
->createQueryBuilder('o')
59-
->select('o.id')
6088
->andWhere('o.createdAt < :date')
6189
->setParameter('date', new \DateTimeImmutable(sprintf('-%d days', $this->daysToKeep)))
62-
->setMaxResults(500)
63-
->getQuery()
64-
->getScalarResult()
6590
;
6691
}
6792
}

0 commit comments

Comments
 (0)