M3: Reorder + Inspiration – Schritt 1 (Logik): Reorder-UseCase, Inspiration-Query (ADR-006), Adopt-UseCase + Unit-Tests

This commit is contained in:
2026-09-19 15:57:13 +02:00
parent ce77785003
commit 52a34225fa
11 changed files with 686 additions and 0 deletions
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Logic\Essen\UseCase;
use App\Logic\Common\ClockInterface;
use App\Logic\Common\EventDispatcherInterface;
use App\Logic\Common\TransactionManagerInterface;
use App\Logic\Essen\Dto\MahlzeitCreateRequest;
use App\Logic\Essen\Dto\MahlzeitResponse;
use App\Logic\Essen\Event\MahlzeitAdoptedEvent;
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
use App\Logic\Essen\Mapping\MahlzeitFactory;
/**
* Use-Case: ein Gericht (z. B. aus dem Archiv) per 1-Klick als NEUEN
* `geplant`-Eintrag übernehmen (Kopie – `gegessenAm` bleibt leer, neue ID,
* am Ende der Planungsliste angehängt).
*
* Wirft `MahlzeitNotFoundException` (→ 404), wenn die Quelle unbekannt ist.
*/
final readonly class MahlzeitAdoptUseCase
{
public function __construct(
private TransactionManagerInterface $tx,
private MahlzeitManagerInterface $mahlzeiten,
private MahlzeitFactory $factory,
private EventDispatcherInterface $events,
private ClockInterface $clock,
) {
}
public function execute(int $quellId): MahlzeitResponse
{
$quelle = $this->mahlzeiten->find($quellId);
$req = new MahlzeitCreateRequest(
name: $quelle->name,
notiz: $quelle->notiz,
wochentage: $quelle->wochentage,
);
$mahlzeit = $this->factory->createFromCreateRequest($req, $this->clock->now());
$gespeichert = $this->tx->execute(fn () => $this->mahlzeiten->create($mahlzeit));
$this->events->dispatch(new MahlzeitAdoptedEvent(id: $gespeichert->id, name: $gespeichert->name, quellId: $quellId));
return MahlzeitResponse::fromModel($gespeichert);
}
}