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

Open in your IDE?
  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\Routing\Annotation\LoginRequired;
  10. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  11. use Shopware\Core\Framework\Routing\Annotation\Since;
  12. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  13. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  16. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedHook;
  17. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoader;
  18. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedHook;
  19. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. /**
  24.  * @Route(defaults={"_routeScope"={"storefront"}})
  25.  *
  26.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  27.  */
  28. class AccountProfileController extends StorefrontController
  29. {
  30.     private AccountOverviewPageLoader $overviewPageLoader;
  31.     private AccountProfilePageLoader $profilePageLoader;
  32.     private AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute;
  33.     private AbstractChangePasswordRoute $changePasswordRoute;
  34.     private AbstractChangeEmailRoute $changeEmailRoute;
  35.     private AbstractDeleteCustomerRoute $deleteCustomerRoute;
  36.     private LoggerInterface $logger;
  37.     /**
  38.      * @internal
  39.      */
  40.     public function __construct(
  41.         AccountOverviewPageLoader $overviewPageLoader,
  42.         AccountProfilePageLoader $profilePageLoader,
  43.         AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute,
  44.         AbstractChangePasswordRoute $changePasswordRoute,
  45.         AbstractChangeEmailRoute $changeEmailRoute,
  46.         AbstractDeleteCustomerRoute $deleteCustomerRoute,
  47.         LoggerInterface $logger
  48.     ) {
  49.         $this->overviewPageLoader $overviewPageLoader;
  50.         $this->profilePageLoader $profilePageLoader;
  51.         $this->changeCustomerProfileRoute $changeCustomerProfileRoute;
  52.         $this->changePasswordRoute $changePasswordRoute;
  53.         $this->changeEmailRoute $changeEmailRoute;
  54.         $this->deleteCustomerRoute $deleteCustomerRoute;
  55.         $this->logger $logger;
  56.     }
  57.     /**
  58.      * @Since("6.0.0.0")
  59.      * @Route("/account", name="frontend.account.home.page", methods={"GET"}, defaults={"_loginRequired"=true})
  60.      * @NoStore
  61.      */
  62.     public function index(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  63.     {
  64.         $page $this->overviewPageLoader->load($request$context$customer);
  65.         $this->hook(new AccountOverviewPageLoadedHook($page$context));
  66.         return $this->renderStorefront('@Storefront/storefront/page/account/index.html.twig', ['page' => $page]);
  67.     }
  68.     /**
  69.      * @Since("6.0.0.0")
  70.      * @Route("/account/profile", name="frontend.account.profile.page", methods={"GET"}, defaults={"_loginRequired"=true})
  71.      * @NoStore
  72.      */
  73.     public function profileOverview(Request $requestSalesChannelContext $context): Response
  74.     {
  75.         $page $this->profilePageLoader->load($request$context);
  76.         $this->hook(new AccountProfilePageLoadedHook($page$context));
  77.         return $this->renderStorefront('@Storefront/storefront/page/account/profile/index.html.twig', [
  78.             'page' => $page,
  79.             'passwordFormViolation' => $request->get('passwordFormViolation'),
  80.             'emailFormViolation' => $request->get('emailFormViolation'),
  81.         ]);
  82.     }
  83.     /**
  84.      * @Since("6.0.0.0")
  85.      * @Route("/account/profile", name="frontend.account.profile.save", methods={"POST"}, defaults={"_loginRequired"=true})
  86.      */
  87.     public function saveProfile(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  88.     {
  89.         try {
  90.             $this->changeCustomerProfileRoute->change($data$context$customer);
  91.             $this->addFlash(self::SUCCESS$this->trans('account.profileUpdateSuccess'));
  92.         } catch (ConstraintViolationException $formViolations) {
  93.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations]);
  94.         } catch (\Exception $exception) {
  95.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  96.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  97.         }
  98.         return $this->redirectToRoute('frontend.account.profile.page');
  99.     }
  100.     /**
  101.      * @Since("6.0.0.0")
  102.      * @Route("/account/profile/email", name="frontend.account.profile.email.save", methods={"POST"}, defaults={"_loginRequired"=true})
  103.      */
  104.     public function saveEmail(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  105.     {
  106.         try {
  107.             $this->changeEmailRoute->change($data->get('email')->toRequestDataBag(), $context$customer);
  108.             $this->addFlash(self::SUCCESS$this->trans('account.emailChangeSuccess'));
  109.         } catch (ConstraintViolationException $formViolations) {
  110.             $this->addFlash(self::DANGER$this->trans('account.emailChangeNoSuccess'));
  111.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'emailFormViolation' => true]);
  112.         } catch (\Exception $exception) {
  113.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  114.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  115.         }
  116.         return $this->redirectToRoute('frontend.account.profile.page');
  117.     }
  118.     /**
  119.      * @Since("6.0.0.0")
  120.      * @Route("/account/profile/password", name="frontend.account.profile.password.save", methods={"POST"}, defaults={"_loginRequired"=true})
  121.      */
  122.     public function savePassword(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  123.     {
  124.         try {
  125.             $this->changePasswordRoute->change($data->get('password')->toRequestDataBag(), $context$customer);
  126.             $this->addFlash(self::SUCCESS$this->trans('account.passwordChangeSuccess'));
  127.         } catch (ConstraintViolationException $formViolations) {
  128.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  129.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'passwordFormViolation' => true]);
  130.         }
  131.         return $this->redirectToRoute('frontend.account.profile.page');
  132.     }
  133.     /**
  134.      * @Since("6.3.3.0")
  135.      * @Route("/account/profile/delete", name="frontend.account.profile.delete", methods={"POST"}, defaults={"_loginRequired"=true})
  136.      */
  137.     public function deleteProfile(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  138.     {
  139.         try {
  140.             $this->deleteCustomerRoute->delete($context$customer);
  141.             $this->addFlash(self::SUCCESS$this->trans('account.profileDeleteSuccessAlert'));
  142.         } catch (\Exception $exception) {
  143.             $this->logger->error($exception->getMessage(), ['e' => $exception]);
  144.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  145.         }
  146.         if ($request->get('redirectTo') || $request->get('forwardTo')) {
  147.             return $this->createActionResponse($request);
  148.         }
  149.         return $this->redirectToRoute('frontend.home.page');
  150.     }
  151. }