vendor/shopware/storefront/Page/Account/Login/AccountLoginPageLoader.php line 69

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Account\Login;
  3. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  9. use Shopware\Core\System\Country\CountryCollection;
  10. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\Salutation\SalesChannel\AbstractSalutationRoute;
  13. use Shopware\Core\System\Salutation\SalutationCollection;
  14. use Shopware\Core\System\Salutation\SalutationEntity;
  15. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. /**
  19.  * Do not use direct or indirect repository calls in a PageLoader. Always use a store-api route to get or put data.
  20.  */
  21. #[Package('customer-order')]
  22. class AccountLoginPageLoader
  23. {
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         private readonly GenericPageLoaderInterface $genericLoader,
  29.         private readonly EventDispatcherInterface $eventDispatcher,
  30.         private readonly AbstractCountryRoute $countryRoute,
  31.         private readonly AbstractSalutationRoute $salutationRoute
  32.     ) {
  33.     }
  34.     /**
  35.      * @throws CategoryNotFoundException
  36.      * @throws InconsistentCriteriaIdsException
  37.      * @throws MissingRequestParameterException
  38.      */
  39.     public function load(Request $requestSalesChannelContext $salesChannelContext): AccountLoginPage
  40.     {
  41.         $page $this->genericLoader->load($request$salesChannelContext);
  42.         $page AccountLoginPage::createFrom($page);
  43.         if ($page->getMetaInformation()) {
  44.             $page->getMetaInformation()->setRobots('noindex,follow');
  45.         }
  46.         $page->setCountries($this->getCountries($salesChannelContext));
  47.         $page->setSalutations($this->getSalutations($salesChannelContext));
  48.         $this->eventDispatcher->dispatch(
  49.             new AccountLoginPageLoadedEvent($page$salesChannelContext$request)
  50.         );
  51.         return $page;
  52.     }
  53.     /**
  54.      * @throws InconsistentCriteriaIdsException
  55.      */
  56.     private function getSalutations(SalesChannelContext $salesChannelContext): SalutationCollection
  57.     {
  58.         $salutations $this->salutationRoute->load(new Request(), $salesChannelContext, new Criteria())->getSalutations();
  59.         $salutations->sort(fn (SalutationEntity $aSalutationEntity $b) => $b->getSalutationKey() <=> $a->getSalutationKey());
  60.         return $salutations;
  61.     }
  62.     private function getCountries(SalesChannelContext $salesChannelContext): CountryCollection
  63.     {
  64.         $criteria = (new Criteria())
  65.             ->addFilter(new EqualsFilter('active'true))
  66.             ->addAssociation('states');
  67.         $countries $this->countryRoute->load(new Request(), $criteria$salesChannelContext)->getCountries();
  68.         $countries->sortCountryAndStates();
  69.         return $countries;
  70.     }
  71. }