update schema module

This commit is contained in:
Marek Lenczewski
2026-04-14 00:43:47 +02:00
parent 4a053c5ee7
commit d576747155
14 changed files with 148 additions and 142 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Helper;
class DateHelper
{
public static function isInRange(\DateTimeImmutable $date): bool
{
$today = new \DateTimeImmutable('today');
return $date >= $today && $date <= $today->modify('+14 days');
}
/** @return array{\DateTimeImmutable, \DateTimeImmutable} */
public static function getDateRange(?\DateTimeImmutable $start, ?\DateTimeImmutable $end): array
{
$today = new \DateTimeImmutable('today');
$from = max($today, $start ?? $today);
$to = min($today->modify('+14 days'), $end ?? $today->modify('+14 days'));
return [$from, $to];
}
public static function getWeeksDiff(\DateTimeImmutable $start, \DateTimeImmutable $date): int
{
return (int) ($start->modify('monday this week')->diff($date->modify('monday this week'))->days / 7);
}
}

View File

@@ -24,11 +24,10 @@ class TaskSchemaRepository extends ServiceEntityRepository
}
/** @return list<TaskSchema> */
public function findActiveWithRepeat(): array
public function findActive(): array
{
return $this->createQueryBuilder('s')
->andWhere('s.status = :status')
->andWhere('s.repeat IS NOT NULL')
->setParameter('status', TaskSchemaStatus::Active)
->getQuery()
->getResult();

View File

@@ -5,6 +5,7 @@ namespace App\Service;
use App\Entity\Task;
use App\Entity\TaskSchema;
use App\Enum\TaskStatus;
use App\Helper\DateHelper;
use App\Repository\TaskSchemaRepository;
use Doctrine\ORM\EntityManagerInterface;
@@ -18,7 +19,7 @@ class TaskGenerator
public function generateNewTasks(): void
{
$schemas = $this->schemaRepo->findActiveWithRepeat();
$schemas = $this->schemaRepo->findActive();
foreach ($schemas as $schema) {
$this->removeTasks($schema);
@@ -50,38 +51,49 @@ class TaskGenerator
}
}
/** @return array{\DateTimeImmutable, \DateTimeImmutable} */
private function getDateRange(TaskSchema $schema): array
{
$today = new \DateTimeImmutable('today');
$from = max($today, $schema->getStart() ?? $today);
$end = min($today->modify('+14 days'), $schema->getEnd() ?? $today->modify('+14 days'));
return [$from, $end];
}
/** @return list<\DateTimeImmutable> */
private function getDates(TaskSchema $schema): array
{
[$from, $end] = $this->getDateRange($schema);
$type = $schema->getRepeatType();
$repeat = $schema->getRepeat();
if ($schema->getRepeatType() === null) {
$date = $schema->getDate();
return DateHelper::isInRange($date) ? [$date] : [];
}
[$from, $end] = DateHelper::getDateRange($schema->getStart(), $schema->getEnd());
$dates = [];
for ($date = $from; $date <= $end; $date = $date->modify('+1 day')) {
if ($type === 'weekly') {
$weekday = (int) $date->format('N') - 1;
if(!$repeat['weekly'][$weekday]) continue;
if ($this->matchesDate($schema, $date)) {
$dates[] = $date;
}
if ($type === 'monthly') {
$monthday = (int) $date->format('j') - 1;
if(!$repeat['monthly'][$monthday]) continue;
}
$dates[] = $date;
}
return $dates;
}
private function matchesDate(TaskSchema $schema, \DateTimeImmutable $date): bool
{
$type = $schema->getRepeatType();
$repeat = $schema->getRepeat();
if ($type === 'daily') {
return true;
}
if ($type === 'weekly' || $type === '2week' || $type === '4week') {
$weekday = (int) $date->format('N') - 1;
if (!$repeat[$type][$weekday]) return false;
if ($type === 'weekly') return true;
$start = $schema->getStart() ?? new \DateTimeImmutable('today');
return DateHelper::getWeeksDiff($start, $date) % ($type === '2week' ? 2 : 4) === 0;
}
if ($type === 'monthly') {
$monthday = (int) $date->format('j') - 1;
return $repeat['monthly'][$monthday];
}
return false;
}
}

View File

@@ -18,10 +18,9 @@ class TaskSchemaManager
public function create(TaskSchemaRequest $req): void
{
if ($req->repeat === null) {
if ($req->repeat === null && $req->date === null) {
$task = new Task();
$task->setName($req->name);
$task->setDate($req->date);
$task->setStatus($req->taskStatus);
$this->em->persist($task);
$this->em->flush();