Feat: Handle uploads independently from form submission

This commit is contained in:
Björn Fromme
2023-09-19 15:28:56 +02:00
parent d6687c3ca4
commit c269880e10
2 changed files with 33 additions and 11 deletions
+31 -11
View File
@@ -8,8 +8,10 @@ use App\Entity\Teamer;
use App\Entity\Upload; use App\Entity\Upload;
use App\Entity\User; use App\Entity\User;
use App\Form\TeamerProfileType; use App\Form\TeamerProfileType;
use App\Model\UploadSessionDto;
use App\Service\Upload\UploadHandler; use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -21,7 +23,8 @@ class ProfileController extends AbstractController
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly ApiClient $apiClient, 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); $this->entityManager->persist($teamer);
} }
// Handle upload independently from form submission to avoid issues with failing validation
$uploadSession = $this->uploadHandler->getUploadSession(); $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 = $this->createForm(TeamerProfileType::class, $teamer, ['upload_session' => $uploadSession]);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { 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 { try {
$bpnPassword = $request->getSession()->get('bpn_password'); $bpnPassword = $request->getSession()->get('bpn_password');
$this->apiClient->updateProfile($user, $bpnPassword); $this->apiClient->updateProfile($user, $bpnPassword);
@@ -65,6 +62,9 @@ class ProfileController extends AbstractController
$this->entityManager->flush(); $this->entityManager->flush();
$this->addFlash('success', 'Deine Daten wurden aktualisiert'); $this->addFlash('success', 'Deine Daten wurden aktualisiert');
$this->logger->info('Update teamer profile', [
'user' => $user->getUserIdentifier(),
]);
return $this->redirectToRoute('app_teamer_profile'); return $this->redirectToRoute('app_teamer_profile');
} }
@@ -74,4 +74,24 @@ class ProfileController extends AbstractController
'teamer' => $teamer, '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(),
]);
}
} }
+2
View File
@@ -0,0 +1,2 @@
*
!.gitignore