reset to setup module state

Remove Task Manager implementation to match `# Setup module` in module.md.
Backend src/ reduced to Kernel.php + empty Entity/, all migrations deleted,
database dropped and recreated. Frontend components/views/services/stores
removed, App.vue/router/style.css reduced to skeletons. CLAUDE.md shortened
to Setup-stand. Old backend/plan.md, plan2.md removed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marek Lenczewski
2026-04-11 13:15:50 +02:00
parent 2cb08331e4
commit 9246ccb5e6
56 changed files with 34 additions and 3951 deletions

View File

@@ -1,61 +0,0 @@
<?php
namespace App\Controller\Api;
use App\DTO\Request\CreateCategoryRequest;
use App\DTO\Request\UpdateCategoryRequest;
use App\Entity\Category;
use App\Repository\CategoryRepository;
use App\Service\CategoryManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api/categories', name: 'categories.')]
class CategoryController extends AbstractController
{
public function __construct(
private CategoryRepository $categoryRepository,
private CategoryManager $categoryManager,
) {}
#[Route('', name: 'index', methods: ['GET'])]
public function index(): JsonResponse
{
$categories = $this->categoryRepository->findAll();
return $this->json($categories, context: ['groups' => ['category:read']]);
}
#[Route('/{id}', name: 'show', methods: ['GET'])]
public function show(Category $category): JsonResponse
{
return $this->json($category, context: ['groups' => ['category:read']]);
}
#[Route('', name: 'create', methods: ['POST'])]
public function create(#[MapRequestPayload] CreateCategoryRequest $dto): JsonResponse
{
$category = $this->categoryManager->createCategory($dto);
return $this->json($category, Response::HTTP_CREATED, context: ['groups' => ['category:read']]);
}
#[Route('/{id}', name: 'update', methods: ['PUT'])]
public function update(#[MapRequestPayload] UpdateCategoryRequest $dto, Category $category): JsonResponse
{
$category = $this->categoryManager->updateCategory($category, $dto);
return $this->json($category, context: ['groups' => ['category:read']]);
}
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
public function delete(Category $category): JsonResponse
{
$this->categoryManager->deleteCategory($category);
return $this->json(null, Response::HTTP_NO_CONTENT);
}
}

View File

@@ -1,75 +0,0 @@
<?php
namespace App\Controller\Api;
use App\DTO\Request\CreateTaskRequest;
use App\DTO\Request\UpdateTaskRequest;
use App\Entity\Task;
use App\Repository\TaskRepository;
use App\Service\TaskGenerator;
use App\Service\TaskManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api/tasks')]
class TaskController extends AbstractController
{
public function __construct(
private TaskManager $taskManager,
private TaskGenerator $taskGenerator,
private TaskRepository $taskRepository,
) {}
#[Route('', name: 'tasks.index', methods: ['GET'])]
public function index(): JsonResponse
{
$today = new \DateTimeImmutable('today');
$end = $today->modify('+6 days');
$this->taskGenerator->generateForRange($today, $end);
$tasks = $this->taskRepository->findAllWithRelations();
return $this->json($tasks, context: ['groups' => ['task:read', 'category:read']]);
}
#[Route('/{id}', name: 'tasks.show', methods: ['GET'])]
public function show(Task $task): JsonResponse
{
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
}
#[Route('', name: 'tasks.create', methods: ['POST'])]
public function create(#[MapRequestPayload] CreateTaskRequest $dto): JsonResponse
{
$task = $this->taskManager->createTask($dto);
return $this->json($task, Response::HTTP_CREATED, context: ['groups' => ['task:read', 'category:read']]);
}
#[Route('/{id}', name: 'tasks.update', methods: ['PUT'])]
public function update(#[MapRequestPayload] UpdateTaskRequest $dto, Task $task): JsonResponse
{
$task = $this->taskManager->updateTask($task, $dto);
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
}
#[Route('/{id}', name: 'tasks.delete', methods: ['DELETE'])]
public function delete(Task $task): JsonResponse
{
$this->taskManager->deleteTask($task);
return $this->json(null, Response::HTTP_NO_CONTENT);
}
#[Route('/{id}/toggle', name: 'tasks.toggle', methods: ['PATCH'])]
public function toggle(Task $task): JsonResponse
{
$task = $this->taskManager->toggleTask($task);
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
}
}

View File

@@ -1,67 +0,0 @@
<?php
namespace App\Controller\Api;
use App\DTO\Request\CreateSchemaRequest;
use App\DTO\Request\UpdateSchemaRequest;
use App\Entity\TaskSchema;
use App\Repository\TaskSchemaRepository;
use App\Service\TaskSchemaManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api/schemas')]
class TaskSchemaController extends AbstractController
{
public function __construct(
private TaskSchemaRepository $schemaRepository,
private TaskSchemaManager $schemaManager,
) {}
#[Route('', name: 'schemas.index', methods: ['GET'])]
public function index(): JsonResponse
{
$schemas = $this->schemaRepository->findBy([], ['createdAt' => 'DESC']);
return $this->json($schemas, context: ['groups' => ['schema:read', 'category:read']]);
}
#[Route('/{id}', name: 'schemas.show', methods: ['GET'])]
public function show(TaskSchema $schema): JsonResponse
{
return $this->json($schema, context: ['groups' => ['schema:read', 'category:read']]);
}
#[Route('', name: 'schemas.create', methods: ['POST'])]
public function create(#[MapRequestPayload] CreateSchemaRequest $dto): JsonResponse
{
$result = $this->schemaManager->createSchema($dto);
if (is_array($result)) {
return $this->json($result, Response::HTTP_CREATED, context: ['groups' => ['task:read', 'category:read']]);
}
return $this->json($result, Response::HTTP_CREATED, context: ['groups' => ['schema:read', 'category:read']]);
}
#[Route('/{id}', name: 'schemas.update', methods: ['PUT'])]
public function update(#[MapRequestPayload] UpdateSchemaRequest $dto, TaskSchema $schema): JsonResponse
{
$schema = $this->schemaManager->updateSchema($schema, $dto);
return $this->json($schema, context: ['groups' => ['schema:read', 'category:read']]);
}
#[Route('/{id}', name: 'schemas.delete', methods: ['DELETE'])]
public function delete(TaskSchema $schema, Request $request): JsonResponse
{
$deleteTasks = (bool) $request->query->get('deleteTasks', false);
$this->schemaManager->deleteSchema($schema, $deleteTasks);
return $this->json(null, Response::HTTP_NO_CONTENT);
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Constraints as Assert;
class CreateCategoryRequest
{
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
public string $name;
#[Assert\NotBlank]
#[Assert\CssColor(formats: Assert\CssColor::HEX_LONG)]
public string $color;
}

View File

@@ -1,21 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Constraints as Assert;
class CreateSchemaRequest
{
use SchemaValidationTrait;
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
public ?string $name = null;
public ?int $categoryId = null;
public ?string $status = null;
public ?string $type = null;
public ?string $startDate = null;
public ?string $endDate = null;
public ?array $days = null;
}

View File

@@ -1,15 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Constraints as Assert;
class CreateTaskRequest
{
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
public ?string $name = null;
public ?int $categoryId = null;
public ?string $date = null;
}

View File

@@ -1,36 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
trait SchemaValidationTrait
{
public function validate(ExecutionContextInterface $context): void
{
if ($this->type !== null && $this->type !== 'single') {
if ($this->startDate !== null && $this->endDate !== null && $this->endDate < $this->startDate) {
$context->buildViolation('endDate muss >= startDate sein.')
->atPath('endDate')
->addViolation();
}
}
if ($this->type === 'single') {
$year = $this->days['year'] ?? null;
if (empty($year)) {
$context->buildViolation('Mindestens ein Datum muss ausgewählt werden.')
->atPath('days')
->addViolation();
}
}
if ($this->type === 'custom') {
if (empty($this->days)) {
$context->buildViolation('days darf nicht leer sein.')
->atPath('days')
->addViolation();
}
}
}
}

View File

@@ -1,14 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Constraints as Assert;
class UpdateCategoryRequest
{
#[Assert\Length(min: 1, max: 255)]
public ?string $name = null;
#[Assert\CssColor(formats: Assert\CssColor::HEX_LONG)]
public ?string $color = null;
}

View File

@@ -1,22 +0,0 @@
<?php
namespace App\DTO\Request;
use Symfony\Component\Validator\Constraints as Assert;
class UpdateSchemaRequest
{
use SchemaValidationTrait;
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
public ?string $name = null;
public ?int $categoryId = null;
public bool $hasCategoryId = false;
public ?string $status = null;
public ?string $type = null;
public ?string $startDate = null;
public ?string $endDate = null;
public ?array $days = null;
}

View File

@@ -1,11 +0,0 @@
<?php
namespace App\DTO\Request;
class UpdateTaskRequest
{
public ?string $name = null;
public ?int $categoryId = null;
public ?string $status = null;
public ?string $date = null;
}

View File

View File

@@ -1,52 +0,0 @@
<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['category:read', 'task:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['category:read', 'category:write', 'task:read'])]
private ?string $name = null;
#[ORM\Column(length: 7)]
#[Groups(['category:read', 'category:write', 'task:read'])]
private ?string $color = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(string $color): static
{
$this->color = $color;
return $this;
}
}

View File

@@ -1,134 +0,0 @@
<?php
namespace App\Entity;
use App\Enum\TaskStatus;
use App\Repository\TaskRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
#[ORM\Entity(repositoryClass: TaskRepository::class)]
class Task
{
public function __construct()
{
$this->createdAt = new \DateTime();
}
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['task:read'])]
private ?int $id = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(name: 'task_id', nullable: true, onDelete: 'SET NULL')]
private ?TaskSchema $schema = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['task:read'])]
private ?string $name = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
#[Groups(['task:read'])]
private ?Category $category = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['task:read'])]
private ?\DateTimeInterface $date = null;
#[ORM\Column(length: 20, enumType: TaskStatus::class)]
#[Groups(['task:read'])]
private TaskStatus $status = TaskStatus::Active;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private \DateTimeInterface $createdAt;
public function getId(): ?int
{
return $this->id;
}
#[Groups(['task:read'])]
#[SerializedName('schemaId')]
public function getSchemaId(): ?int
{
return $this->schema?->getId();
}
public function getSchema(): ?TaskSchema
{
return $this->schema;
}
public function setSchema(?TaskSchema $schema): static
{
$this->schema = $schema;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): static
{
$this->category = $category;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): static
{
$this->date = $date;
return $this;
}
public function getStatus(): TaskStatus
{
return $this->status;
}
public function setStatus(TaskStatus $status): static
{
$this->status = $status;
return $this;
}
#[Groups(['task:read'])]
#[SerializedName('isPast')]
public function isPast(): bool
{
return $this->date !== null && $this->date < new \DateTimeImmutable('today');
}
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
}

View File

@@ -1,147 +0,0 @@
<?php
namespace App\Entity;
use App\Enum\TaskSchemaStatus;
use App\Enum\TaskSchemaType;
use App\Repository\TaskSchemaRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
#[ORM\Entity(repositoryClass: TaskSchemaRepository::class)]
class TaskSchema
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['schema:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['schema:read', 'schema:write'])]
private ?string $name = null;
#[ORM\Column(length: 20, enumType: TaskSchemaStatus::class)]
#[Groups(['schema:read', 'schema:write'])]
private TaskSchemaStatus $status = TaskSchemaStatus::Active;
#[ORM\Column(name: 'task_type', length: 20, enumType: TaskSchemaType::class)]
#[Groups(['schema:read', 'schema:write'])]
#[SerializedName('type')]
private TaskSchemaType $taskType = TaskSchemaType::Single;
#[ORM\ManyToOne]
#[ORM\JoinColumn(onDelete: 'SET NULL')]
#[Groups(['schema:read'])]
private ?Category $category = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['schema:read', 'schema:write'])]
private ?\DateTimeInterface $startDate = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups(['schema:read', 'schema:write'])]
private ?\DateTimeInterface $endDate = null;
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['schema:read', 'schema:write'])]
private ?array $days = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
#[Groups(['schema:read'])]
private \DateTimeInterface $createdAt;
public function __construct()
{
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getStatus(): TaskSchemaStatus
{
return $this->status;
}
public function setStatus(TaskSchemaStatus $status): static
{
$this->status = $status;
return $this;
}
public function getTaskType(): TaskSchemaType
{
return $this->taskType;
}
public function setTaskType(TaskSchemaType $taskType): static
{
$this->taskType = $taskType;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): static
{
$this->category = $category;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(?\DateTimeInterface $startDate): static
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(?\DateTimeInterface $endDate): static
{
$this->endDate = $endDate;
return $this;
}
public function getDays(): ?array
{
return $this->days;
}
public function setDays(?array $days): static
{
$this->days = $days;
return $this;
}
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}
}

View File

@@ -1,9 +0,0 @@
<?php
namespace App\Enum;
enum TaskSchemaStatus: string
{
case Active = 'active';
case Disabled = 'disabled';
}

View File

@@ -1,10 +0,0 @@
<?php
namespace App\Enum;
enum TaskSchemaType: string
{
case Single = 'single';
case Daily = 'daily';
case Custom = 'custom';
}

View File

@@ -1,9 +0,0 @@
<?php
namespace App\Enum;
enum TaskStatus: string
{
case Active = 'active';
case Done = 'done';
}

View File

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Repository;
use App\Entity\Category;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Category>
*/
class CategoryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Category::class);
}
}

View File

@@ -1,107 +0,0 @@
<?php
namespace App\Repository;
use App\Entity\Task;
use App\Entity\TaskSchema;
use App\Enum\TaskSchemaStatus;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Task>
*/
class TaskRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Task::class);
}
/**
* @return Task[]
*/
public function findAllWithRelations(): array
{
return $this->createQueryBuilder('task')
->leftJoin('task.schema', 'schema')
->leftJoin('task.category', 'category')
->addSelect('schema', 'category')
->where('task.schema IS NULL OR schema.status != :disabled')
->setParameter('disabled', TaskSchemaStatus::Disabled)
->orderBy('task.date', 'ASC')
->getQuery()
->getResult();
}
public function findBySchemaAndDate(TaskSchema $schema, \DateTimeInterface $date): ?Task
{
return $this->createQueryBuilder('task')
->where('task.schema = :schema')
->andWhere('task.date = :date')
->setParameter('schema', $schema)
->setParameter('date', $date)
->getQuery()
->getOneOrNullResult();
}
/**
* @return Task[]
*/
public function findInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
{
return $this->createQueryBuilder('task')
->leftJoin('task.schema', 'schema')
->leftJoin('task.category', 'category')
->addSelect('schema', 'category')
->where('task.date >= :from')
->andWhere('task.date <= :to')
->andWhere('task.schema IS NULL OR schema.status != :disabled')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('disabled', TaskSchemaStatus::Disabled)
->orderBy('task.date', 'ASC')
->getQuery()
->getResult();
}
/**
* @return array<string, true> Set of "schemaId-YYYY-MM-DD" keys
*/
public function getExistingKeys(\DateTimeInterface $from, \DateTimeInterface $to): array
{
$rows = $this->createQueryBuilder('task')
->select('IDENTITY(task.schema) AS schemaId', 'task.date')
->where('task.date >= :from')
->andWhere('task.date <= :to')
->andWhere('task.schema IS NOT NULL')
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
->getArrayResult();
$set = [];
foreach ($rows as $row) {
$date = $row['date'] instanceof \DateTimeInterface
? $row['date']->format('Y-m-d')
: $row['date'];
$set[$row['schemaId'] . '-' . $date] = true;
}
return $set;
}
/**
* @return Task[]
*/
public function findBySchemaFromDate(TaskSchema $schema, \DateTimeInterface $fromDate): array
{
return $this->createQueryBuilder('task')
->where('task.schema = :schema')
->andWhere('task.date >= :from')
->setParameter('schema', $schema)
->setParameter('from', $fromDate)
->getQuery()
->getResult();
}
}

View File

@@ -1,38 +0,0 @@
<?php
namespace App\Repository;
use App\Entity\TaskSchema;
use App\Enum\TaskSchemaStatus;
use App\Enum\TaskSchemaType;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<TaskSchema>
*/
class TaskSchemaRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TaskSchema::class);
}
/**
* @return TaskSchema[]
*/
public function findActiveSchemasInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
{
return $this->createQueryBuilder('t')
->where('t.status = :active')
->andWhere('t.taskType IN (:types)')
->andWhere('t.startDate <= :to')
->andWhere('t.endDate IS NULL OR t.endDate >= :from')
->setParameter('active', TaskSchemaStatus::Active)
->setParameter('types', [TaskSchemaType::Daily, TaskSchemaType::Custom])
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
->getResult();
}
}

View File

@@ -1,43 +0,0 @@
<?php
namespace App\Service;
use App\DTO\Request\CreateCategoryRequest;
use App\DTO\Request\UpdateCategoryRequest;
use App\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;
class CategoryManager
{
public function __construct(
private EntityManagerInterface $em,
) {}
public function createCategory(CreateCategoryRequest $request): Category
{
$category = new Category();
$category->setName($request->name);
$category->setColor($request->color);
$this->em->persist($category);
$this->em->flush();
return $category;
}
public function updateCategory(Category $category, UpdateCategoryRequest $request): Category
{
$category->setName($request->name);
$category->setColor($request->color);
$this->em->flush();
return $category;
}
public function deleteCategory(Category $category): void
{
$this->em->remove($category);
$this->em->flush();
}
}

View File

@@ -1,75 +0,0 @@
<?php
namespace App\Service;
use App\Entity\TaskSchema;
use App\Enum\TaskSchemaType;
class DeadlineCalculator
{
/**
* @return \DateTimeInterface[]
*/
public function getDeadlinesForRange(TaskSchema $schema, \DateTimeInterface $from, \DateTimeInterface $to): array
{
$startDate = $schema->getStartDate();
if ($startDate === null) {
return [];
}
$effectiveFrom = $from > $startDate ? $from : $startDate;
$endDate = $schema->getEndDate();
$effectiveTo = $endDate !== null && $to > $endDate ? $endDate : $to;
if ($effectiveFrom > $effectiveTo) {
return [];
}
$deadlines = [];
$current = new \DateTimeImmutable($effectiveFrom->format('Y-m-d'));
$end = new \DateTimeImmutable($effectiveTo->format('Y-m-d'));
while ($current <= $end) {
if ($this->matchesType($schema, $current)) {
$deadlines[] = $current;
}
$current = $current->modify('+1 day');
}
return $deadlines;
}
private function matchesType(TaskSchema $schema, \DateTimeImmutable $date): bool
{
return match ($schema->getTaskType()) {
TaskSchemaType::Daily => true,
TaskSchemaType::Custom => $this->matchesDays($schema, $date),
default => false,
};
}
private function matchesDays(TaskSchema $schema, \DateTimeImmutable $date): bool
{
$days = $schema->getDays() ?? [];
if (!empty($days['week']) && in_array((int) $date->format('N'), $days['week'], true)) {
return true;
}
if (!empty($days['month']) && in_array((int) $date->format('j'), $days['month'], true)) {
return true;
}
if (!empty($days['year'])) {
$month = (int) $date->format('n');
$day = (int) $date->format('j');
foreach ($days['year'] as $yd) {
if (($yd['month'] ?? 0) === $month && ($yd['day'] ?? 0) === $day) {
return true;
}
}
}
return false;
}
}

View File

@@ -1,49 +0,0 @@
<?php
namespace App\Service;
use App\Entity\Task;
use App\Repository\TaskRepository;
use App\Repository\TaskSchemaRepository;
use Doctrine\ORM\EntityManagerInterface;
class TaskGenerator
{
public function __construct(
private EntityManagerInterface $em,
private TaskSchemaRepository $schemaRepository,
private TaskRepository $taskRepository,
private DeadlineCalculator $deadlineCalculator,
) {}
public function generateForRange(\DateTimeInterface $from, \DateTimeInterface $to): void
{
$schemas = $this->schemaRepository->findActiveSchemasInRange($from, $to);
$existingKeys = $this->taskRepository->getExistingKeys($from, $to);
$hasNew = false;
foreach ($schemas as $schema) {
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $from, $to);
foreach ($deadlines as $deadline) {
$key = $schema->getId() . '-' . $deadline->format('Y-m-d');
if (!isset($existingKeys[$key])) {
$task = new Task();
$task->setSchema($schema);
$task->setDate(new \DateTime($deadline->format('Y-m-d')));
$task->setName($schema->getName());
$task->setCategory($schema->getCategory());
$this->em->persist($task);
$existingKeys[$key] = true;
$hasNew = true;
}
}
}
if ($hasNew) {
$this->em->flush();
}
}
}

View File

@@ -1,76 +0,0 @@
<?php
namespace App\Service;
use App\DTO\Request\CreateTaskRequest;
use App\DTO\Request\UpdateTaskRequest;
use App\Entity\Task;
use App\Enum\TaskStatus;
use App\Repository\CategoryRepository;
use Doctrine\ORM\EntityManagerInterface;
class TaskManager
{
public function __construct(
private EntityManagerInterface $em,
private CategoryRepository $categoryRepository,
) {}
public function createTask(CreateTaskRequest $request): Task
{
$task = new Task();
$task->setName($request->name);
if ($request->categoryId !== null) {
$category = $this->categoryRepository->find($request->categoryId);
$task->setCategory($category);
}
if ($request->date !== null) {
$task->setDate(new \DateTime($request->date));
}
$this->em->persist($task);
$this->em->flush();
return $task;
}
public function updateTask(Task $task, UpdateTaskRequest $request): Task
{
$task->setName($request->name);
$category = $request->categoryId !== null
? $this->categoryRepository->find($request->categoryId)
: null;
$task->setCategory($category);
$status = TaskStatus::tryFrom($request->status);
if ($status !== null) {
$task->setStatus($status);
}
$task->setDate($request->date ? new \DateTime($request->date) : null);
$this->em->flush();
return $task;
}
public function toggleTask(Task $task): Task
{
$newStatus = $task->getStatus() === TaskStatus::Active
? TaskStatus::Done
: TaskStatus::Active;
$task->setStatus($newStatus);
$this->em->flush();
return $task;
}
public function deleteTask(Task $task): void
{
$this->em->remove($task);
$this->em->flush();
}
}

View File

@@ -1,150 +0,0 @@
<?php
namespace App\Service;
use App\DTO\Request\CreateSchemaRequest;
use App\DTO\Request\UpdateSchemaRequest;
use App\Entity\Task;
use App\Entity\TaskSchema;
use App\Enum\TaskSchemaStatus;
use App\Enum\TaskSchemaType;
use App\Repository\CategoryRepository;
use App\Repository\TaskRepository;
use Doctrine\ORM\EntityManagerInterface;
class TaskSchemaManager
{
public function __construct(
private EntityManagerInterface $em,
private CategoryRepository $categoryRepository,
private TaskRepository $taskRepository,
private TaskSynchronizer $taskSynchronizer,
) {}
public function createSchema(CreateSchemaRequest $request): TaskSchema|array
{
$schema = new TaskSchema();
$schema->setName($request->name ?? '');
$this->applyFields($schema, $request);
$this->resolveCategory($schema, $request);
$this->applyDefaults($schema);
$this->em->persist($schema);
$this->em->flush();
if ($schema->getTaskType() === TaskSchemaType::Single) {
$tasks = $this->createSingleTasks($schema);
$this->em->remove($schema);
$this->em->flush();
return $tasks;
}
return $schema;
}
public function updateSchema(TaskSchema $schema, UpdateSchemaRequest $request): TaskSchema
{
if ($request->name !== null) {
$schema->setName($request->name);
}
$this->applyFields($schema, $request);
$this->resolveCategory($schema, $request);
$this->applyDefaults($schema);
$this->em->flush();
$this->taskSynchronizer->syncForSchema($schema);
return $schema;
}
public function deleteSchema(TaskSchema $schema, bool $deleteTasks = false): void
{
if ($deleteTasks) {
$tasks = $this->taskRepository->findBy(['schema' => $schema]);
foreach ($tasks as $task) {
$this->em->remove($task);
}
}
$this->em->remove($schema);
$this->em->flush();
}
private function applyFields(TaskSchema $schema, CreateSchemaRequest|UpdateSchemaRequest $request): void
{
if ($request->status !== null) {
$status = TaskSchemaStatus::tryFrom($request->status);
if ($status !== null) {
$schema->setStatus($status);
}
}
if ($request->type !== null) {
$taskType = TaskSchemaType::tryFrom($request->type);
if ($taskType !== null) {
$schema->setTaskType($taskType);
}
}
$schema->setStartDate($request->startDate !== null ? new \DateTime($request->startDate) : null);
$schema->setEndDate($request->endDate !== null ? new \DateTime($request->endDate) : null);
$schema->setDays($request->days);
}
private function resolveCategory(TaskSchema $schema, UpdateSchemaRequest|CreateSchemaRequest $request): void
{
if ($request instanceof UpdateSchemaRequest) {
if ($request->hasCategoryId) {
if ($request->categoryId !== null) {
$category = $this->categoryRepository->find($request->categoryId);
$schema->setCategory($category);
} else {
$schema->setCategory(null);
}
}
} else {
if ($request->categoryId !== null) {
$category = $this->categoryRepository->find($request->categoryId);
$schema->setCategory($category);
}
}
}
private function applyDefaults(TaskSchema $schema): void
{
if ($schema->getTaskType() !== TaskSchemaType::Single && $schema->getStartDate() === null) {
$schema->setStartDate(new \DateTime('today'));
}
}
/**
* @return Task[]
*/
private function createSingleTasks(TaskSchema $schema): array
{
$days = $schema->getDays()['year'] ?? [];
$tasks = [];
foreach ($days as $yd) {
$month = $yd['month'] ?? 1;
$day = $yd['day'] ?? 1;
$date = new \DateTime(sprintf('%d-%02d-%02d', (int) date('Y'), $month, $day));
$task = new Task();
$task->setName($schema->getName());
$task->setCategory($schema->getCategory());
$task->setDate($date);
$this->em->persist($task);
$tasks[] = $task;
}
$this->em->flush();
return $tasks;
}
}

View File

@@ -1,106 +0,0 @@
<?php
namespace App\Service;
use App\Entity\Task;
use App\Entity\TaskSchema;
use App\Repository\TaskRepository;
use Doctrine\ORM\EntityManagerInterface;
class TaskSynchronizer
{
public function __construct(
private EntityManagerInterface $em,
private TaskRepository $taskRepository,
private DeadlineCalculator $deadlineCalculator,
) {}
public function syncForSchema(TaskSchema $schema): void
{
$today = new \DateTimeImmutable('today');
$end = $this->calculateSyncEnd($schema, $today);
$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->resetFutureOverrides($schema, $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;
return $end < $minEnd ? $minEnd : $end;
}
/**
* @return array<string, Task>
*/
private function loadExistingByDate(TaskSchema $schema, \DateTimeImmutable $today): array
{
$futureTasks = $this->taskRepository->findBySchemaFromDate($schema, $today);
$existingByDate = [];
foreach ($futureTasks as $task) {
$existingByDate[$task->getDate()->format('Y-m-d')] = $task;
}
return $existingByDate;
}
/**
* @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($task);
unset($existingByDate[$dateKey]);
}
}
}
/**
* @param array<string, Task> $existingByDate
*/
private function resetFutureOverrides(TaskSchema $schema, array $existingByDate): void
{
foreach ($existingByDate as $task) {
$task->setName($schema->getName());
$task->setCategory($schema->getCategory());
}
}
/**
* @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])) {
$task = new Task();
$task->setSchema($schema);
$task->setDate(new \DateTime($dateKey));
$task->setName($schema->getName());
$task->setCategory($schema->getCategory());
$this->em->persist($task);
}
}
}
}