44
55namespace CodeRhapsodie \DataflowBundle \DataflowType ;
66
7+ use CodeRhapsodie \DataflowBundle \Repository \JobRepository ;
78use Psr \Log \LoggerAwareInterface ;
89use Psr \Log \LoggerAwareTrait ;
910use Psr \Log \LoggerInterface ;
@@ -13,6 +14,10 @@ abstract class AbstractDataflowType implements DataflowTypeInterface, LoggerAwar
1314{
1415 use LoggerAwareTrait;
1516
17+ private JobRepository $ repository ;
18+
19+ private ?\DateTime $ saveDate = null ;
20+
1621 /**
1722 * @codeCoverageIgnore
1823 */
@@ -21,14 +26,21 @@ public function getAliases(): iterable
2126 return [];
2227 }
2328
24- public function process (array $ options ): Result
29+ public function process (array $ options, ? int $ jobId = null ): Result
2530 {
2631 $ optionsResolver = new OptionsResolver ();
2732 $ this ->configureOptions ($ optionsResolver );
2833 $ options = $ optionsResolver ->resolve ($ options );
2934
3035 $ builder = $ this ->createDataflowBuilder ();
3136 $ builder ->setName ($ this ->getLabel ());
37+ $ builder ->addAfterItemProcessors (function (int |string $ index , mixed $ item , int $ count ) use ($ jobId ) {
38+ if ($ jobId === null || $ this ->saveDate ->modify ('+1 minute ' ) > new \DateTime ()) {
39+ return ;
40+ }
41+
42+ $ this ->repository ->updateCount ($ jobId , $ count );
43+ });
3244 $ this ->buildDataflow ($ builder , $ options );
3345 $ dataflow = $ builder ->getDataflow ();
3446 if ($ dataflow instanceof LoggerAwareInterface && $ this ->logger instanceof LoggerInterface) {
@@ -51,4 +63,9 @@ protected function configureOptions(OptionsResolver $optionsResolver): void
5163 }
5264
5365 abstract protected function buildDataflow (DataflowBuilder $ builder , array $ options ): void ;
66+
67+ public function setRepository (JobRepository $ repository ): void
68+ {
69+ $ this ->repository = $ repository ;
70+ }
5471}
0 commit comments