src/Storefront/Controller/AccountProfileController.php line 38

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeCustomerProfileRoute;
  6. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeEmailRoute;
  7. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangePasswordRoute;
  8. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractDeleteCustomerRoute;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  11. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedHook;
  14. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoader;
  15. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedHook;
  16. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @internal
  22.  */
  23. #[Route(defaults: ['_routeScope' => ['storefront']])]
  24. #[Package('storefront')]
  25. class AccountProfileController extends StorefrontController
  26. {
  27.     /**
  28.      * @internal
  29.      */
  30.     public function __construct(private readonly AccountOverviewPageLoader $overviewPageLoader, private readonly AccountProfilePageLoader $profilePageLoader, private readonly AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute, private readonly AbstractChangePasswordRoute $changePasswordRoute, private readonly AbstractChangeEmailRoute $changeEmailRoute, private readonly AbstractDeleteCustomerRoute $deleteCustomerRoute, private readonly LoggerInterface $logger)
  31.     {
  32.     }
  33.     #[Route(path'/account'name'frontend.account.home.page'defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  34.     public function index(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  35.     {
  36.         $page $this->overviewPageLoader->load($request$context$customer);
  37.         $this->hook(new AccountOverviewPageLoadedHook($page$context));
  38.         return $this->renderStorefront('@Storefront/storefront/page/account/index.html.twig', ['page' => $page]);
  39.     }
  40.     #[Route(path'/account/profile'name'frontend.account.profile.page'defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  41.     public function profileOverview(Request $requestSalesChannelContext $context): Response
  42.     {
  43.         $page $this->profilePageLoader->load($request$context);
  44.         $this->hook(new AccountProfilePageLoadedHook($page$context));
  45.         return $this->renderStorefront('@Storefront/storefront/page/account/profile/index.html.twig', [
  46.             'page' => $page,
  47.             'passwordFormViolation' => $request->get('passwordFormViolation'),
  48.             'emailFormViolation' => $request->get('emailFormViolation'),
  49.         ]);
  50.     }
  51.     #[Route(path'/account/profile'name'frontend.account.profile.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  52.     public function saveProfile(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  53.     {
  54.         try {
  55.             $this->changeCustomerProfileRoute->change($data$context$customer);
  56.             $this->addFlash(self::SUCCESS$this->trans('account.profileUpdateSuccess'));
  57.         } catch (ConstraintViolationException $formViolations) {
  58.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations]);
  59.         } catch (\Exception $exception) {
  60.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  61.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  62.         }
  63.         return $this->redirectToRoute('frontend.account.profile.page');
  64.     }
  65.     #[Route(path'/account/profile/email'name'frontend.account.profile.email.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  66.     public function saveEmail(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  67.     {
  68.         try {
  69.             $this->changeEmailRoute->change($data->get('email')->toRequestDataBag(), $context$customer);
  70.             $this->addFlash(self::SUCCESS$this->trans('account.emailChangeSuccess'));
  71.         } catch (ConstraintViolationException $formViolations) {
  72.             $this->addFlash(self::DANGER$this->trans('account.emailChangeNoSuccess'));
  73.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'emailFormViolation' => true]);
  74.         } catch (\Exception $exception) {
  75.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  76.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  77.         }
  78.         return $this->redirectToRoute('frontend.account.profile.page');
  79.     }
  80.     #[Route(path'/account/profile/password'name'frontend.account.profile.password.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  81.     public function savePassword(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  82.     {
  83.         try {
  84.             $this->changePasswordRoute->change($data->get('password')->toRequestDataBag(), $context$customer);
  85.             $this->addFlash(self::SUCCESS$this->trans('account.passwordChangeSuccess'));
  86.         } catch (ConstraintViolationException $formViolations) {
  87.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  88.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'passwordFormViolation' => true]);
  89.         }
  90.         return $this->redirectToRoute('frontend.account.profile.page');
  91.     }
  92.     #[Route(path'/account/profile/delete'name'frontend.account.profile.delete'defaults: ['_loginRequired' => true], methods: ['POST'])]
  93.     public function deleteProfile(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  94.     {
  95.         try {
  96.             $this->deleteCustomerRoute->delete($context$customer);
  97.             $this->addFlash(self::SUCCESS$this->trans('account.profileDeleteSuccessAlert'));
  98.         } catch (\Exception $exception) {
  99.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  100.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  101.         }
  102.         if ($request->get('redirectTo') || $request->get('forwardTo')) {
  103.             return $this->createActionResponse($request);
  104.         }
  105.         return $this->redirectToRoute('frontend.home.page');
  106.     }
  107. }