feat: implement screendesign
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Account;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use App\Entity\User;
|
||||
use App\Form\PersonalDataType;
|
||||
use App\Security\Crypt;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
/**
|
||||
* Controller for managing customer personal data operations.
|
||||
*
|
||||
* Provides functionality for viewing and updating customer profile information
|
||||
* through integration with the BusProNet API system. Handles personal data
|
||||
* management and newsletter subscription preferences for authenticated users.
|
||||
*/
|
||||
class PersonalDataController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @param ApiClient $apiClient BusProNet API client for data operations
|
||||
* @param Crypt $crypt Encryption service for password handling
|
||||
* @param LoggerInterface $logger Logger for audit trails and debugging
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly Crypt $crypt,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Display and handle updates to customer personal data.
|
||||
*
|
||||
* Fetches current personal data from BusProNet API and displays an editable form.
|
||||
* Handles form submission to update personal information including address and
|
||||
* communication details. Uses the Post-Redirect-Get pattern for form processing.
|
||||
*
|
||||
* @param Request $request The HTTP request containing form data
|
||||
*
|
||||
* @return Response The rendered personal data page or redirect response
|
||||
*
|
||||
* @throws ApiClientException When BusProNet API communication fails
|
||||
*/
|
||||
#[Route('/personal-data', name: 'app_personal_data')]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$email = $user->getEmail();
|
||||
$password = $this->crypt->decrypt($user->getPassword());
|
||||
|
||||
try {
|
||||
$personalData = $this
|
||||
->apiClient
|
||||
->getPersonalData($email, $password);
|
||||
} catch (ApiClientException $e) {
|
||||
$this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden');
|
||||
$personalData = new PersonalData();
|
||||
}
|
||||
|
||||
if ($personalData instanceof Notification) {
|
||||
$this->logger->error('Unable to fetch personal data', [
|
||||
'code' => $personalData->code,
|
||||
'error' => $personalData->message,
|
||||
]);
|
||||
|
||||
$this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden');
|
||||
$personalData = new PersonalData();
|
||||
}
|
||||
|
||||
$personalDataForm = $this->createForm(PersonalDataType::class, $personalData, [
|
||||
'attr' => ['novalidate' => 'novalidate'],
|
||||
'validation_groups' => ['personal_data'],
|
||||
]);
|
||||
$personalDataForm->handleRequest($request);
|
||||
|
||||
if ($personalDataForm->isSubmitted() && $personalDataForm->isValid()) {
|
||||
try {
|
||||
$this->apiClient->updatePersonalData($email, $password, $personalData);
|
||||
$this->addFlash('success', 'Deine persönlichen Daten wurden aktualisiert');
|
||||
$this->logger->info('Updated personal data', [
|
||||
'email' => $user->getEmail(),
|
||||
]);
|
||||
} catch (ApiClientException $e) {
|
||||
$this->addFlash('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_personal_data');
|
||||
}
|
||||
|
||||
return $this->render('account/personal_data.html.twig', [
|
||||
'personalData' => $personalData,
|
||||
'personalDataForm' => $personalDataForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle newsletter subscription status for the authenticated user.
|
||||
*
|
||||
* Retrieves current personal data, toggles the newsletter subscription flag,
|
||||
* and updates the preference via BusProNet API. Designed for HTMX AJAX
|
||||
* requests to provide immediate feedback without full page reload.
|
||||
*
|
||||
* @return Response Redirect response to personal data page
|
||||
*
|
||||
* @throws ApiClientException When BusProNet API communication fails
|
||||
*/
|
||||
#[Route('/personal-data/newsletter', name: 'app_personal_data_newsletter', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function newsletter(): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$email = $user->getEmail();
|
||||
$password = $this->crypt->decrypt($user->getPassword());
|
||||
|
||||
try {
|
||||
$personalData = $this
|
||||
->apiClient
|
||||
->getPersonalData($email, $password);
|
||||
} catch (ApiClientException $e) {
|
||||
$this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden');
|
||||
$personalData = new PersonalData();
|
||||
}
|
||||
|
||||
$personalData->communication->newsletter = !$personalData->communication->newsletter;
|
||||
|
||||
try {
|
||||
$this->apiClient->updateNewsletterRegistration($email, $password, $personalData);
|
||||
$this->addFlash('success', 'Deine Anmeldung zum Newsletter wurde aktualisiert');
|
||||
$this->logger->info('Updated newsletter registration', [
|
||||
'email' => $user->getEmail(),
|
||||
]);
|
||||
} catch (ApiClientException $e) {
|
||||
$this->addFlash('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_personal_data');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user