/
ProcessFactory.php
123 lines (103 loc) · 3.45 KB
/
ProcessFactory.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Concrete\Core\Command\Process;
use Concrete\Core\Command\Batch\Batch as PendingBatch;
use Concrete\Core\Command\Batch\BatchUpdater;
use Concrete\Core\Command\Batch\Stamp\BatchStamp;
use Concrete\Core\Command\Task\Input\InputInterface;
use Concrete\Core\Command\Task\TaskInterface;
use Concrete\Core\Entity\Command\Batch;
use Concrete\Core\Entity\Command\Process;
use Concrete\Core\Entity\Command\TaskProcess;
use Concrete\Core\Localization\Service\Date;
use Concrete\Core\User\User;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Messenger\MessageBusInterface;
class ProcessFactory
{
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var Date
*/
protected $dateService;
/**
* @var MessageBusInterface
*/
protected $messageBus;
/**
* @var BatchUpdater
*/
protected $batchUpdater;
public function __construct(
EntityManager $entityManager,
Date $dateService,
MessageBusInterface $messageBus,
BatchUpdater $batchUpdater
) {
$this->entityManager = $entityManager;
$this->dateService = $dateService;
$this->messageBus = $messageBus;
$this->batchUpdater = $batchUpdater;
}
public function createTaskProcess(TaskInterface $task, InputInterface $input = null)
{
$process = new TaskProcess();
$process->setTask($task);
$process->setName($task->getController()->getName());
$process->setInput($input);
return $this->fillProcess($process);
}
public function createProcess(string $name): Process
{
$process = new Process();
$process->setName($name);
return $this->fillProcess($process);
}
protected function fillProcess(Process $process): Process
{
$process->setDateStarted($this->dateService->toDateTime()->getTimestamp());
$user = new User();
if ($user) {
$userInfo = $user->getUserInfoObject();
if ($userInfo) {
$process->setUser($userInfo->getEntityObject());
}
}
$this->entityManager->persist($process);
$this->entityManager->flush();
return $process;
}
public function createBatchEntity(PendingBatch $batch): Batch
{
$batchEntity = new Batch();
$this->entityManager->persist($batchEntity);
$this->entityManager->flush();
return $batchEntity;
}
public function setBatchTotal(Batch $batchEntity, Process $process, $totalJobs)
{
$this->batchUpdater->updateJobs($batchEntity->getID(), BatchUpdater::COLUMN_TOTAL, $totalJobs);
$this->batchUpdater->updateJobs($batchEntity->getID(), BatchUpdater::COLUMN_PENDING, $totalJobs);
$this->entityManager->refresh($batchEntity);
$this->entityManager->persist($process);
$this->entityManager->flush();
}
/**
* @param PendingBatch $batch
*/
public function createWithBatch(PendingBatch $batch): Process
{
$batchEntity = $this->createBatchEntity($batch);
$process = $this->createProcess($batch->getName());
$process->setBatch($batchEntity);
$total = 0;
foreach ($batch->getWrappedMessages($batchEntity) as $message) {
$this->messageBus->dispatch($message, [new BatchStamp($batchEntity->getId())]);
$total++;
}
$this->setBatchTotal($batchEntity, $process, $total);
return $process;
}
}