M2: Status & Wochentage – Schritt 1 (Logik): CheckOff/Uncheck UseCases + Status-Event + Tests
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Logic\Essen\Event;
|
||||
|
||||
use App\Logic\Essen\Model\MahlzeitStatus;
|
||||
|
||||
/**
|
||||
* Domain-Event: der Status einer Mahlzeit wurde geändert (nach erfolgreichem Commit).
|
||||
*
|
||||
* Trägt den neuen Status + den (ggf. leeren) gegessenAm-Zeitpunkt, damit ein
|
||||
* Subscriber (z. B. Archiv, Statistik) ohne erneuten DB-Zugriff reagieren kann.
|
||||
*/
|
||||
final readonly class MahlzeitStatusChangedEvent
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public MahlzeitStatus $status,
|
||||
public ?\DateTimeImmutable $gegessenAm,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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\MahlzeitResponse;
|
||||
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
|
||||
|
||||
/**
|
||||
* Use-Case: eine Mahlzeit abhaken (geplant → gegessen).
|
||||
*
|
||||
* Idempotent: Eine bereits gegessene Mahlzeit bleibt unverändert (derselbe
|
||||
* gegessenAm-Zeitpunkt bleibt erhalten). Wirft `MahlzeitNotFoundException`
|
||||
* (→ 404), wenn die ID unbekannt ist.
|
||||
*/
|
||||
final readonly class MahlzeitCheckOffUseCase
|
||||
{
|
||||
public function __construct(
|
||||
private TransactionManagerInterface $tx,
|
||||
private MahlzeitManagerInterface $mahlzeiten,
|
||||
private EventDispatcherInterface $events,
|
||||
private ClockInterface $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(int $id): MahlzeitResponse
|
||||
{
|
||||
$jetzt = $this->clock->now();
|
||||
$bestehend = $this->mahlzeiten->find($id);
|
||||
$markiert = $bestehend->markAsGegessen($jetzt);
|
||||
|
||||
$gespeichert = $this->tx->execute(fn () => $this->mahlzeiten->update($markiert));
|
||||
|
||||
$this->events->dispatch(new MahlzeitStatusChangedEvent(
|
||||
id: $id,
|
||||
name: $gespeichert->name,
|
||||
status: $gespeichert->status,
|
||||
gegessenAm: $gespeichert->gegessenAm,
|
||||
));
|
||||
|
||||
return MahlzeitResponse::fromModel($gespeichert);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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\MahlzeitResponse;
|
||||
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
|
||||
|
||||
/**
|
||||
* Use-Case: eine Mahlzeit wieder aufheben (gegessen → geplant).
|
||||
*
|
||||
* Setzt `gegessenAm` zurück. Idempotent: Eine bereits geplante Mahlzeit bleibt
|
||||
* unverändert. Wirft `MahlzeitNotFoundException` (→ 404), wenn die ID unbekannt ist.
|
||||
*/
|
||||
final readonly class MahlzeitUncheckUseCase
|
||||
{
|
||||
public function __construct(
|
||||
private TransactionManagerInterface $tx,
|
||||
private MahlzeitManagerInterface $mahlzeiten,
|
||||
private EventDispatcherInterface $events,
|
||||
private ClockInterface $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(int $id): MahlzeitResponse
|
||||
{
|
||||
$jetzt = $this->clock->now();
|
||||
$bestehend = $this->mahlzeiten->find($id);
|
||||
$markiert = $bestehend->markAsGeplant($jetzt);
|
||||
|
||||
$gespeichert = $this->tx->execute(fn () => $this->mahlzeiten->update($markiert));
|
||||
|
||||
$this->events->dispatch(new MahlzeitStatusChangedEvent(
|
||||
id: $id,
|
||||
name: $gespeichert->name,
|
||||
status: $gespeichert->status,
|
||||
gegessenAm: $gespeichert->gegessenAm,
|
||||
));
|
||||
|
||||
return MahlzeitResponse::fromModel($gespeichert);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user