vendor/shopware/storefront/Controller/AccountProfileController.php line 46

  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.  * Do not use direct or indirect repository calls in a controller. Always use a store-api route to get or put data
  23.  */
  24. #[Route(defaults: ['_routeScope' => ['storefront']])]
  25. #[Package('storefront')]
  26. class AccountProfileController extends StorefrontController
  27. {
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(
  32.         private readonly AccountOverviewPageLoader $overviewPageLoader,
  33.         private readonly AccountProfilePageLoader $profilePageLoader,
  34.         private readonly AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute,
  35.         private readonly AbstractChangePasswordRoute $changePasswordRoute,
  36.         private readonly AbstractChangeEmailRoute $changeEmailRoute,
  37.         private readonly AbstractDeleteCustomerRoute $deleteCustomerRoute,
  38.         private readonly LoggerInterface $logger
  39.     ) {
  40.     }
  41.     #[Route(path'/account'name'frontend.account.home.page'defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  42.     public function index(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  43.     {
  44.         $page $this->overviewPageLoader->load($request$context$customer);
  45.         $this->hook(new AccountOverviewPageLoadedHook($page$context));
  46.         return $this->renderStorefront('@Storefront/storefront/page/account/index.html.twig', ['page' => $page]);
  47.     }
  48.     #[Route(path'/account/profile'name'frontend.account.profile.page'defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  49.     public function profileOverview(Request $requestSalesChannelContext $context): Response
  50.     {
  51.         $page $this->profilePageLoader->load($request$context);
  52.         $this->hook(new AccountProfilePageLoadedHook($page$context));
  53.         return $this->renderStorefront('@Storefront/storefront/page/account/profile/index.html.twig', [
  54.             'page' => $page,
  55.             'passwordFormViolation' => $request->get('passwordFormViolation'),
  56.             'emailFormViolation' => $request->get('emailFormViolation'),
  57.         ]);
  58.     }
  59.     #[Route(path'/account/profile'name'frontend.account.profile.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  60.     public function saveProfile(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  61.     {
  62.         try {
  63.             $this->changeCustomerProfileRoute->change($data$context$customer);
  64.             $this->addFlash(self::SUCCESS$this->trans('account.profileUpdateSuccess'));
  65.         } catch (ConstraintViolationException $formViolations) {
  66.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations]);
  67.         } catch (\Exception $exception) {
  68.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  69.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  70.         }
  71.         return $this->redirectToRoute('frontend.account.profile.page');
  72.     }
  73.     #[Route(path'/account/profile/email'name'frontend.account.profile.email.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  74.     public function saveEmail(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  75.     {
  76.         try {
  77.             $this->changeEmailRoute->change($data->get('email')->toRequestDataBag(), $context$customer);
  78.             $this->addFlash(self::SUCCESS$this->trans('account.emailChangeSuccess'));
  79.         } catch (ConstraintViolationException $formViolations) {
  80.             $this->addFlash(self::DANGER$this->trans('account.emailChangeNoSuccess'));
  81.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'emailFormViolation' => true]);
  82.         } catch (\Exception $exception) {
  83.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  84.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  85.         }
  86.         return $this->redirectToRoute('frontend.account.profile.page');
  87.     }
  88.     #[Route(path'/account/profile/password'name'frontend.account.profile.password.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  89.     public function savePassword(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  90.     {
  91.         try {
  92.             $this->changePasswordRoute->change($data->get('password')->toRequestDataBag(), $context$customer);
  93.             $this->addFlash(self::SUCCESS$this->trans('account.passwordChangeSuccess'));
  94.         } catch (ConstraintViolationException $formViolations) {
  95.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  96.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'passwordFormViolation' => true]);
  97.         }
  98.         return $this->redirectToRoute('frontend.account.profile.page');
  99.     }
  100.     #[Route(path'/account/profile/delete'name'frontend.account.profile.delete'defaults: ['_loginRequired' => true], methods: ['POST'])]
  101.     public function deleteProfile(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  102.     {
  103.         try {
  104.             $this->deleteCustomerRoute->delete($context$customer);
  105.             $this->addFlash(self::SUCCESS$this->trans('account.profileDeleteSuccessAlert'));
  106.         } catch (\Exception $exception) {
  107.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  108.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  109.         }
  110.         if ($request->get('redirectTo') || $request->get('forwardTo')) {
  111.             return $this->createActionResponse($request);
  112.         }
  113.         return $this->redirectToRoute('frontend.home.page');
  114.     }
  115. }