44
55namespace Setono \SyliusConversionAttributionPlugin \Command ;
66
7+ use Doctrine \ORM \QueryBuilder ;
78use Doctrine \Persistence \ManagerRegistry ;
89use Setono \Doctrine \ORMTrait ;
910use Setono \SyliusConversionAttributionPlugin \Model \SourceInterface ;
1011use Symfony \Component \Console \Attribute \AsCommand ;
1112use Symfony \Component \Console \Command \Command ;
1213use Symfony \Component \Console \Input \InputInterface ;
1314use Symfony \Component \Console \Output \OutputInterface ;
15+ use Symfony \Component \Console \Style \SymfonyStyle ;
1416use 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