M1: CRUD-Slice 'Mahlzeit' Schritt 1 – Logik + Daten + Tests
Rich-Model Mahlzeit (MahlzeitStatus, Wochentag) mit Invarianten und Zustandsübergängen; DTOs + MahlzeitFactory (reine Mapping-Funktionen); Manager (create hängt an Ende an: maxSortOrder+1, find → 404) und Create/Update/Delete-UseCases mit Transaktion + Events nach Commit. Data-Layer: MahlzeitEntity (Tabelle essens_mahlzeiten, wochentage als CSV), Mapper (CSV/Enum/null), Provider (find/list/maxSortOrder), Processor (create/save/delete). DI-Bindings in services.yaml. ClockInterface (Logic) + SystemClock (Data). Tests: Model/Factory/UseCase-Unit + Provider/Processor/Mapper-Integration (In-Mem-SQLite). composer test grün (53 Tests, 161 Assertions).
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Data\Essen\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* Doctrine-Entity für `essens_mahlzeiten`.
|
||||
*
|
||||
* Bewusst "dumm": nur Getter/Setter, keine Fachlogik (die lebt im Rich-Model
|
||||
* `App\Logic\Essen\Model\Mahlzeit`). `wochentage` wird als CSV-String
|
||||
* ("mo,di,fr") persistiert – das Mapping ins `Wochentag[]`-Array macht der Mapper.
|
||||
*/
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'essens_mahlzeiten')]
|
||||
#[ORM\Index(columns: ['status'], name: 'idx_status')]
|
||||
#[ORM\Index(columns: ['sort_order'], name: 'idx_sort_order')]
|
||||
final class MahlzeitEntity
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $name = '';
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $notiz = null;
|
||||
|
||||
/** CSV der Wochentag-Kurzformen, z. B. "mo,di,fr". */
|
||||
#[ORM\Column(type: 'string', length: 32, nullable: true)]
|
||||
private ?string $wochentage = null;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $sortOrder = 0;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 32)]
|
||||
private string $status = 'geplant';
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?\DateTimeImmutable $gegessenAm = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $erstelltAm;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $aktualisiertAm;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$now = new \DateTimeImmutable();
|
||||
$this->erstelltAm = $now;
|
||||
$this->aktualisiertAm = $now;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): self
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getNotiz(): ?string
|
||||
{
|
||||
return $this->notiz;
|
||||
}
|
||||
|
||||
public function setNotiz(?string $notiz): self
|
||||
{
|
||||
$this->notiz = $notiz;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWochentage(): ?string
|
||||
{
|
||||
return $this->wochentage;
|
||||
}
|
||||
|
||||
public function setWochentage(?string $wochentage): self
|
||||
{
|
||||
$this->wochentage = $wochentage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSortOrder(): int
|
||||
{
|
||||
return $this->sortOrder;
|
||||
}
|
||||
|
||||
public function setSortOrder(int $sortOrder): self
|
||||
{
|
||||
$this->sortOrder = $sortOrder;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus(string $status): self
|
||||
{
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGegessenAm(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->gegessenAm;
|
||||
}
|
||||
|
||||
public function setGegessenAm(?\DateTimeImmutable $gegessenAm): self
|
||||
{
|
||||
$this->gegessenAm = $gegessenAm;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getErstelltAm(): \DateTimeImmutable
|
||||
{
|
||||
return $this->erstelltAm;
|
||||
}
|
||||
|
||||
public function setErstelltAm(\DateTimeImmutable $erstelltAm): self
|
||||
{
|
||||
$this->erstelltAm = $erstelltAm;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAktualisiertAm(): \DateTimeImmutable
|
||||
{
|
||||
return $this->aktualisiertAm;
|
||||
}
|
||||
|
||||
public function setAktualisiertAm(\DateTimeImmutable $aktualisiertAm): self
|
||||
{
|
||||
$this->aktualisiertAm = $aktualisiertAm;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Data\Essen\Mapper;
|
||||
|
||||
use App\Data\Essen\Entity\MahlzeitEntity;
|
||||
use App\Logic\Essen\Model\Mahlzeit;
|
||||
use App\Logic\Essen\Model\MahlzeitStatus;
|
||||
use App\Logic\Essen\Model\Wochentag;
|
||||
|
||||
/**
|
||||
* Stateless-Mapping zwischen `MahlzeitEntity` und dem Rich-Model `Mahlzeit`.
|
||||
*
|
||||
* Hier leben die "unbequemen" Persistenz-Konvertierungen:
|
||||
* - `wochentage`: CSV-String ("mo,di,fr") ↔ `Wochentag[]`
|
||||
* - `status`: String ("geplant") ↔ `MahlzeitStatus`-Enum
|
||||
* - `notiz`: nullable-String ↔ nicht-leerer String
|
||||
*/
|
||||
final class MahlzeitMapper
|
||||
{
|
||||
public function toModel(MahlzeitEntity $entity): Mahlzeit
|
||||
{
|
||||
return new Mahlzeit(
|
||||
id: (int) $entity->getId(),
|
||||
name: $entity->getName(),
|
||||
notiz: (string) $entity->getNotiz(),
|
||||
wochentage: $this->csvToWochentage((string) $entity->getWochentage()),
|
||||
sortOrder: $entity->getSortOrder(),
|
||||
status: MahlzeitStatus::from($entity->getStatus()),
|
||||
erstelltAm: $entity->getErstelltAm(),
|
||||
aktualisiertAm: $entity->getAktualisiertAm(),
|
||||
gegessenAm: $entity->getGegessenAm(),
|
||||
);
|
||||
}
|
||||
|
||||
public function createEntity(Mahlzeit $model): MahlzeitEntity
|
||||
{
|
||||
$entity = new MahlzeitEntity();
|
||||
$this->apply($model, $entity);
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function updateEntity(Mahlzeit $model, MahlzeitEntity $entity): void
|
||||
{
|
||||
$this->apply($model, $entity);
|
||||
}
|
||||
|
||||
private function apply(Mahlzeit $model, MahlzeitEntity $entity): void
|
||||
{
|
||||
$entity->setName($model->name);
|
||||
$entity->setNotiz($model->notiz === '' ? null : $model->notiz);
|
||||
$entity->setWochentage($this->wochentageToCsv($model->wochentage()));
|
||||
$entity->setSortOrder($model->sortOrder);
|
||||
$entity->setStatus($model->status->value);
|
||||
$entity->setGegessenAm($model->gegessenAm);
|
||||
$entity->setErstelltAm($model->erstelltAm);
|
||||
$entity->setAktualisiertAm($model->aktualisiertAm);
|
||||
}
|
||||
|
||||
/** @return list<Wochentag> */
|
||||
private function csvToWochentage(string $csv): array
|
||||
{
|
||||
if (\trim($csv) === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$tage = [];
|
||||
foreach (explode(',', $csv) as $wert) {
|
||||
$tag = Wochentag::fromValue($wert);
|
||||
if ($tag !== null) {
|
||||
$tage[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
return $tage;
|
||||
}
|
||||
|
||||
/** @param list<Wochentag> $tage */
|
||||
private function wochentageToCsv(array $tage): ?string
|
||||
{
|
||||
if ($tage === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return implode(',', Wochentag::zuWerten($tage));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Data\Essen\Processor;
|
||||
|
||||
use App\Data\Essen\Entity\MahlzeitEntity;
|
||||
use App\Data\Essen\Mapper\MahlzeitMapper;
|
||||
use App\Logic\Essen\Exception\MahlzeitNotFoundException;
|
||||
use App\Logic\Essen\MahlzeitProcessorInterface;
|
||||
use App\Logic\Essen\Model\Mahlzeit;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* Write-Zugriff auf den Mahlzeiten-Datenbestand (Doctrine-Implementierung).
|
||||
*
|
||||
* `create` persistiert ein neues Modell und liefert es mit der neuen DB-ID zurück.
|
||||
* `save`/`delete` setzen die ID voraus und werfen `MahlzeitNotFoundException` (→ 404).
|
||||
*/
|
||||
final class MahlzeitProcessor implements MahlzeitProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly MahlzeitMapper $mapper,
|
||||
) {
|
||||
}
|
||||
|
||||
public function create(Mahlzeit $mahlzeit): Mahlzeit
|
||||
{
|
||||
$entity = $this->mapper->createEntity($mahlzeit);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->mapper->toModel($entity);
|
||||
}
|
||||
|
||||
public function save(Mahlzeit $mahlzeit): Mahlzeit
|
||||
{
|
||||
$entity = $this->findEntity($mahlzeit->id);
|
||||
$this->mapper->updateEntity($mahlzeit, $entity);
|
||||
$this->em->flush();
|
||||
|
||||
return $mahlzeit;
|
||||
}
|
||||
|
||||
public function delete(Mahlzeit $mahlzeit): void
|
||||
{
|
||||
$entity = $this->findEntity($mahlzeit->id);
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function findEntity(int $id): MahlzeitEntity
|
||||
{
|
||||
$entity = $this->em->getRepository(MahlzeitEntity::class)->find($id);
|
||||
|
||||
if ($entity === null) {
|
||||
throw new MahlzeitNotFoundException($id);
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Data\Essen\Provider;
|
||||
|
||||
use App\Data\Essen\Entity\MahlzeitEntity;
|
||||
use App\Data\Essen\Mapper\MahlzeitMapper;
|
||||
use App\Logic\Essen\MahlzeitProviderInterface;
|
||||
use App\Logic\Essen\Model\Mahlzeit;
|
||||
use App\Logic\Essen\Model\MahlzeitStatus;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* Read-Zugriff auf den Mahlzeiten-Datenbestand (Doctrine-Implementierung).
|
||||
*/
|
||||
final class MahlzeitProvider implements MahlzeitProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly MahlzeitMapper $mapper,
|
||||
) {
|
||||
}
|
||||
|
||||
public function find(int $id): ?Mahlzeit
|
||||
{
|
||||
$entity = $this->em->getRepository(MahlzeitEntity::class)->find($id);
|
||||
|
||||
return $entity instanceof MahlzeitEntity ? $this->mapper->toModel($entity) : null;
|
||||
}
|
||||
|
||||
/** @return list<Mahlzeit> */
|
||||
public function list(?MahlzeitStatus $status = null): array
|
||||
{
|
||||
$qb = $this->em->getRepository(MahlzeitEntity::class)->createQueryBuilder('m');
|
||||
|
||||
if ($status !== null) {
|
||||
$qb->andWhere('m.status = :status')->setParameter('status', $status->value);
|
||||
}
|
||||
|
||||
$qb->orderBy('m.sortOrder', 'ASC')->addOrderBy('m.id', 'ASC');
|
||||
|
||||
/** @var MahlzeitEntity[] $entities */
|
||||
$entities = $qb->getQuery()->getResult();
|
||||
|
||||
$mahlzeiten = [];
|
||||
foreach ($entities as $entity) {
|
||||
$mahlzeiten[] = $this->mapper->toModel($entity);
|
||||
}
|
||||
|
||||
return $mahlzeiten;
|
||||
}
|
||||
|
||||
public function maxSortOrder(): int
|
||||
{
|
||||
$max = $this->em
|
||||
->getRepository(MahlzeitEntity::class)
|
||||
->createQueryBuilder('m')
|
||||
->select('MAX(m.sortOrder)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
|
||||
return $max === null ? 0 : (int) $max;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Data\Infrastructure;
|
||||
|
||||
use App\Logic\Common\ClockInterface;
|
||||
|
||||
/**
|
||||
* Bridge: liefert "jetzt" aus der Systemuhr für die Logik-Layer.
|
||||
*/
|
||||
final class SystemClock implements ClockInterface
|
||||
{
|
||||
public function now(): \DateTimeImmutable
|
||||
{
|
||||
return new \DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user