103 lines
3.7 KiB
PHP
103 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Teamer\Profile;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\ApiClientException;
|
|
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;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
class IndexController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly ValidatorInterface $validator,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly ApiClient $apiClient,
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
#[Route('/teamer/profile', name: 'app_teamer_profile_index')]
|
|
#[IsGranted('ROLE_TEAMER')]
|
|
public function index(Request $request): Response
|
|
{
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
$teamer = $user->getTeamer();
|
|
|
|
if (null === $teamer) {
|
|
$teamer = new Teamer();
|
|
$user->setTeamer($teamer);
|
|
$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);
|
|
}
|
|
|
|
// Validate teamer data to show missing data right away
|
|
$errors = $this->validator->validate($teamer, null, ['profile_preflight']);
|
|
|
|
$form = $this->createForm(TeamerProfileType::class, $teamer, ['upload_session' => $uploadSession]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
try {
|
|
$bpnPassword = $request->getSession()->get('bpn_password');
|
|
$this->apiClient->updateProfile($user, $bpnPassword);
|
|
$this->entityManager->flush();
|
|
} catch (ApiClientException $e) {
|
|
}
|
|
|
|
$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_index');
|
|
}
|
|
|
|
return $this->render('teamer/profile/index.html.twig', [
|
|
'form' => $form->createView(),
|
|
'teamer' => $teamer,
|
|
'errors' => $errors,
|
|
]);
|
|
}
|
|
|
|
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(),
|
|
]);
|
|
}
|
|
} |