vendor/shopware/platform/src/Storefront/Controller/AccountProfileController.php line 82

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