From c269880e10365825c13db1e6d08544c86f48d94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Tue, 19 Sep 2023 15:28:56 +0200 Subject: [PATCH] Feat: Handle uploads independently from form submission --- src/Controller/Teamer/ProfileController.php | 42 +++++++++++++++------ uploads/.gitignore | 2 + 2 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 uploads/.gitignore diff --git a/src/Controller/Teamer/ProfileController.php b/src/Controller/Teamer/ProfileController.php index 627a688..13ea545 100644 --- a/src/Controller/Teamer/ProfileController.php +++ b/src/Controller/Teamer/ProfileController.php @@ -8,8 +8,10 @@ use App\Entity\Teamer; use App\Entity\Upload; use App\Entity\User; use App\Form\TeamerProfileType; +use App\Model\UploadSessionDto; use App\Service\Upload\UploadHandler; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -21,7 +23,8 @@ class ProfileController extends AbstractController public function __construct( private readonly EntityManagerInterface $entityManager, private readonly ApiClient $apiClient, - private readonly UploadHandler $uploadHandler + private readonly UploadHandler $uploadHandler, + private readonly LoggerInterface $logger ) { } @@ -39,22 +42,16 @@ class ProfileController extends AbstractController $this->entityManager->persist($teamer); } + // Handle upload independently from form submission to avoid issues with failing validation $uploadSession = $this->uploadHandler->getUploadSession(); + if (true === $request->isMethod('POST') && 0 < $uploadSession->getCount()) { + $this->updatePhoto($user, $uploadSession); + } $form = $this->createForm(TeamerProfileType::class, $teamer, ['upload_session' => $uploadSession]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - if ($uploadSession->getCount() > 0) { - if (null !== $existingUpload = $teamer->getPhoto()) { - $this->entityManager->remove($existingUpload); - } - $upload = Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, Upload::TYPE_PHOTO); - $teamer->setPhoto($upload); - $this->uploadHandler->moveUploadSessionFilesFromOrphanage('photo', $uploadSession); - $this->uploadHandler->destroyUploadSession(); - } - try { $bpnPassword = $request->getSession()->get('bpn_password'); $this->apiClient->updateProfile($user, $bpnPassword); @@ -65,6 +62,9 @@ class ProfileController extends AbstractController $this->entityManager->flush(); $this->addFlash('success', 'Deine Daten wurden aktualisiert'); + $this->logger->info('Update teamer profile', [ + 'user' => $user->getUserIdentifier(), + ]); return $this->redirectToRoute('app_teamer_profile'); } @@ -74,4 +74,24 @@ class ProfileController extends AbstractController 'teamer' => $teamer, ]); } + + private function updatePhoto(User $user, UploadSessionDto $uploadSession): void + { + $teamer = $user->getTeamer(); + + if (null !== $existingUpload = $teamer->getPhoto()) { + $this->entityManager->remove($existingUpload); + } + + $upload = Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, Upload::TYPE_PHOTO); + $teamer->setPhoto($upload); + $this->entityManager->flush(); + $this->uploadHandler->moveUploadSessionFilesFromOrphanage('photo', $uploadSession); + $this->uploadHandler->destroyUploadSession(); + + $this->addFlash('success', 'Dein Profilbild wurde aktualisiert'); + $this->logger->info('Update teamer photo', [ + 'user' => $user->getUserIdentifier(), + ]); + } } \ No newline at end of file diff --git a/uploads/.gitignore b/uploads/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/uploads/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore