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

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