<?php declare(strict_types=1);
namespace Shopware\Storefront\Page\Account\Login;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\System\Country\CountryCollection;
use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\Salutation\SalesChannel\AbstractSalutationRoute;
use Shopware\Core\System\Salutation\SalutationCollection;
use Shopware\Core\System\Salutation\SalutationEntity;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
#[Package('customer-order')]
class AccountLoginPageLoader
{
/**
* @var GenericPageLoaderInterface
*/
private $genericLoader;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var AbstractCountryRoute
*/
private $countryRoute;
/**
* @var AbstractSalutationRoute
*/
private $salutationRoute;
/**
* @internal
*/
public function __construct(
GenericPageLoaderInterface $genericLoader,
EventDispatcherInterface $eventDispatcher,
AbstractCountryRoute $countryRoute,
AbstractSalutationRoute $salutationRoute
) {
$this->genericLoader = $genericLoader;
$this->eventDispatcher = $eventDispatcher;
$this->countryRoute = $countryRoute;
$this->salutationRoute = $salutationRoute;
}
/**
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function load(Request $request, SalesChannelContext $salesChannelContext): AccountLoginPage
{
$page = $this->genericLoader->load($request, $salesChannelContext);
$page = AccountLoginPage::createFrom($page);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,follow');
}
$page->setCountries($this->getCountries($salesChannelContext));
$page->setSalutations($this->getSalutations($salesChannelContext));
$this->eventDispatcher->dispatch(
new AccountLoginPageLoadedEvent($page, $salesChannelContext, $request)
);
return $page;
}
/**
* @throws InconsistentCriteriaIdsException
*/
private function getSalutations(SalesChannelContext $salesChannelContext): SalutationCollection
{
$salutations = $this->salutationRoute->load(new Request(), $salesChannelContext, new Criteria())->getSalutations();
$salutations->sort(function (SalutationEntity $a, SalutationEntity $b) {
return $b->getSalutationKey() <=> $a->getSalutationKey();
});
return $salutations;
}
private function getCountries(SalesChannelContext $salesChannelContext): CountryCollection
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('active', true))
->addAssociation('states');
$countries = $this->countryRoute->load(new Request(), $criteria, $salesChannelContext)->getCountries();
$countries->sortCountryAndStates();
return $countries;
}
}