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

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