-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractDataflowType.php
More file actions
73 lines (57 loc) · 2 KB
/
AbstractDataflowType.php
File metadata and controls
73 lines (57 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace CodeRhapsodie\DataflowBundle\DataflowType;
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
abstract class AbstractDataflowType implements DataflowTypeInterface, AutoUpdateCountInterface
{
use LoggerAwareTrait;
private JobRepository $repository;
private ?\DateTime $saveDate = null;
/**
* @codeCoverageIgnore
*/
public function getAliases(): iterable
{
return [];
}
public function process(array $options, ?int $jobId = null): Result
{
$this->saveDate = new \DateTime('+1 minute');
$optionsResolver = new OptionsResolver();
$this->configureOptions($optionsResolver);
$options = $optionsResolver->resolve($options);
$builder = $this->createDataflowBuilder();
$builder->setName($this->getLabel());
$builder->addAfterItemProcessor(function (int|string $index, mixed $item, int $count) use ($jobId): void {
if ($jobId === null || $this->saveDate > new \DateTime()) {
return;
}
$this->repository->updateCount($jobId, $count);
$this->saveDate = new \DateTime('+1 minute');
});
$this->buildDataflow($builder, $options);
$dataflow = $builder->getDataflow();
if ($this->logger instanceof LoggerInterface) {
$dataflow->setLogger($this->logger);
}
return $dataflow->process();
}
public function setRepository(JobRepository $repository): void
{
$this->repository = $repository;
}
protected function createDataflowBuilder(): DataflowBuilder
{
return new DataflowBuilder();
}
/**
* @codeCoverageIgnore
*/
protected function configureOptions(OptionsResolver $optionsResolver): void
{
}
abstract protected function buildDataflow(DataflowBuilder $builder, array $options): void;
}