Extract buildInvitationOrder to reduce createInvitation returns from 4 to 3

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-24 13:35:19 +01:00
parent ee2cc6d0d7
commit b6a3a629f7

View File

@@ -752,53 +752,13 @@ class AccountController extends AbstractController
$this->requireEventOwnership($event);
$firstName = trim($request->request->getString('first_name'));
$lastName = trim($request->request->getString('last_name'));
$email = trim($request->request->getString('email'));
$items = $request->request->all('items');
$result = $this->buildInvitationOrder($event, $request, $em);
$redirect = $this->redirectToRoute('app_account_edit_event', ['id' => $event->getId(), 'tab' => 'invitations']);
if ('' === $firstName || '' === $lastName || '' === $email || 0 === \count($items)) {
$this->addFlash('error', 'Tous les champs sont requis.');
return $redirect;
}
$count = $em->getRepository(BilletBuyer::class)->count([]) + 1;
$order = new BilletBuyer();
$order->setEvent($event);
$order->setFirstName($firstName);
$order->setLastName($lastName);
$order->setEmail($email);
$order->setOrderNumber(date('Y-m-d').'-'.$count);
$order->setTotalHT(0);
$order->setIsInvitation(true);
foreach ($items as $itemData) {
$billetId = (int) ($itemData['billet_id'] ?? 0);
$qty = max(1, (int) ($itemData['quantity'] ?? 1));
$billet = $em->getRepository(Billet::class)->find($billetId);
if (!$billet || $billet->getCategory()->getEvent()->getId() !== $event->getId()) {
continue;
}
$item = new BilletBuyerItem();
$item->setBillet($billet);
$item->setBilletName($billet->getName());
$item->setQuantity($qty);
$item->setUnitPriceHT(0);
$order->addItem($item);
}
if ($order->getItems()->isEmpty()) {
$this->addFlash('error', 'Aucun billet valide selectionne.');
return $redirect;
if ($result instanceof Response) {
return $result;
}
$order = $result;
$em->persist($order);
$em->flush();
@@ -1143,6 +1103,57 @@ class AccountController extends AbstractController
]);
}
private function buildInvitationOrder(Event $event, Request $request, EntityManagerInterface $em): BilletBuyer|Response
{
$firstName = trim($request->request->getString('first_name'));
$lastName = trim($request->request->getString('last_name'));
$email = trim($request->request->getString('email'));
$items = $request->request->all('items');
$redirect = $this->redirectToRoute('app_account_edit_event', ['id' => $event->getId(), 'tab' => 'invitations']);
if ('' === $firstName || '' === $lastName || '' === $email || 0 === \count($items)) {
$this->addFlash('error', 'Tous les champs sont requis.');
return $redirect;
}
$count = $em->getRepository(BilletBuyer::class)->count([]) + 1;
$order = new BilletBuyer();
$order->setEvent($event);
$order->setFirstName($firstName);
$order->setLastName($lastName);
$order->setEmail($email);
$order->setOrderNumber(date('Y-m-d').'-'.$count);
$order->setTotalHT(0);
$order->setIsInvitation(true);
foreach ($items as $itemData) {
$billetId = (int) ($itemData['billet_id'] ?? 0);
$qty = max(1, (int) ($itemData['quantity'] ?? 1));
$billet = $em->getRepository(Billet::class)->find($billetId);
if (!$billet || $billet->getCategory()->getEvent()->getId() !== $event->getId()) {
continue;
}
$item = new BilletBuyerItem();
$item->setBillet($billet);
$item->setBilletName($billet->getName());
$item->setQuantity($qty);
$item->setUnitPriceHT(0);
$order->addItem($item);
}
if ($order->getItems()->isEmpty()) {
$this->addFlash('error', 'Aucun billet valide selectionne.');
return $redirect;
}
return $order;
}
private function requireEventOwnership(Event $event): User
{
/** @var User $user */