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

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