update
This commit is contained in:
@@ -19,74 +19,106 @@ class TaskSynchronizer
|
||||
public function syncForSchema(TaskSchema $schema): void
|
||||
{
|
||||
$today = new \DateTimeImmutable('today');
|
||||
$end = $this->calculateSyncEnd($schema, $today);
|
||||
|
||||
// Range: bis endDate oder mindestens +6 Tage
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
|
||||
$shouldExist = [];
|
||||
foreach ($deadlines as $deadline) {
|
||||
$shouldExist[$deadline->format('Y-m-d')] = true;
|
||||
}
|
||||
|
||||
$existingByDate = $this->loadExistingByDate($schema, $today);
|
||||
|
||||
$this->removeObsoleteTasks($existingByDate, $shouldExist);
|
||||
$this->handleNullDateTasks($schema);
|
||||
$this->resetFutureOverrides($existingByDate);
|
||||
$this->createMissingTasks($schema, $deadlines, $existingByDate);
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function calculateSyncEnd(TaskSchema $schema, \DateTimeImmutable $today): \DateTimeImmutable
|
||||
{
|
||||
$minEnd = $today->modify('+6 days');
|
||||
$end = $schema->getEndDate()
|
||||
? new \DateTimeImmutable($schema->getEndDate()->format('Y-m-d'))
|
||||
: $minEnd;
|
||||
if ($end < $minEnd) {
|
||||
$end = $minEnd;
|
||||
}
|
||||
|
||||
// Soll-Termine berechnen
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
|
||||
$shouldExist = [];
|
||||
foreach ($deadlines as $dl) {
|
||||
$shouldExist[$dl->format('Y-m-d')] = true;
|
||||
}
|
||||
return $end < $minEnd ? $minEnd : $end;
|
||||
}
|
||||
|
||||
// Alle zukünftigen Tasks laden (mit Datum)
|
||||
$futureTasks = $this->taskRepository->findByTaskFromDate($schema, $today);
|
||||
/**
|
||||
* @return array<string, Task>
|
||||
*/
|
||||
private function loadExistingByDate(TaskSchema $schema, \DateTimeImmutable $today): array
|
||||
{
|
||||
$futureTasks = $this->taskRepository->findBySchemaFromDate($schema, $today);
|
||||
$existingByDate = [];
|
||||
foreach ($futureTasks as $occ) {
|
||||
$existingByDate[$occ->getDate()->format('Y-m-d')] = $occ;
|
||||
foreach ($futureTasks as $task) {
|
||||
$existingByDate[$task->getDate()->format('Y-m-d')] = $task;
|
||||
}
|
||||
|
||||
// Null-Datum Tasks laden
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
return $existingByDate;
|
||||
}
|
||||
|
||||
// Nicht mehr im Schema -> entfernen
|
||||
foreach ($existingByDate as $dateKey => $occ) {
|
||||
/**
|
||||
* @param array<string, Task> $existingByDate
|
||||
* @param array<string, true> $shouldExist
|
||||
*/
|
||||
private function removeObsoleteTasks(array &$existingByDate, array $shouldExist): void
|
||||
{
|
||||
foreach ($existingByDate as $dateKey => $task) {
|
||||
if (!isset($shouldExist[$dateKey])) {
|
||||
$this->em->remove($occ);
|
||||
$this->em->remove($task);
|
||||
unset($existingByDate[$dateKey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function handleNullDateTasks(TaskSchema $schema): void
|
||||
{
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
// Einzel ohne Deadline: null-date Task behalten
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single && $schema->getDeadline() === null) {
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory(null);
|
||||
$task->setCategoryOverridden(false);
|
||||
}
|
||||
} else {
|
||||
// Sonst null-date Tasks entfernen
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$this->em->remove($occ);
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$this->em->remove($task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bestehende zukünftige Overrides zurücksetzen
|
||||
foreach ($existingByDate as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
/**
|
||||
* @param array<string, Task> $existingByDate
|
||||
*/
|
||||
private function resetFutureOverrides(array $existingByDate): void
|
||||
{
|
||||
foreach ($existingByDate as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory(null);
|
||||
$task->setCategoryOverridden(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Fehlende Tasks erstellen
|
||||
foreach ($deadlines as $dl) {
|
||||
$dateKey = $dl->format('Y-m-d');
|
||||
/**
|
||||
* @param \DateTimeInterface[] $deadlines
|
||||
* @param array<string, Task> $existingByDate
|
||||
*/
|
||||
private function createMissingTasks(TaskSchema $schema, array $deadlines, array $existingByDate): void
|
||||
{
|
||||
foreach ($deadlines as $deadline) {
|
||||
$dateKey = $deadline->format('Y-m-d');
|
||||
if (!isset($existingByDate[$dateKey])) {
|
||||
$occ = new Task();
|
||||
$occ->setSchema($schema);
|
||||
$occ->setDate(new \DateTime($dateKey));
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(new \DateTime($dateKey));
|
||||
|
||||
$this->em->persist($occ);
|
||||
$this->em->persist($task);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user