Feat: Handle uploads independently from form submission
This commit is contained in:
@@ -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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user