vendor/shopware/storefront/Page/Account/Overview/AccountOverviewPageLoader.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Account\Overview;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractCustomerRoute;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
  8. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\Framework\Feature;
  13. use Shopware\Core\Framework\Log\Package;
  14. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
  17. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  18. use Shopware\Storefront\Pagelet\Newsletter\Account\NewsletterAccountPageletLoader;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. #[Package('customer-order')]
  22. class AccountOverviewPageLoader
  23. {
  24.     /**
  25.      * @var GenericPageLoaderInterface
  26.      */
  27.     private $genericLoader;
  28.     /**
  29.      * @var EventDispatcherInterface
  30.      */
  31.     private $eventDispatcher;
  32.     /**
  33.      * @var AbstractOrderRoute
  34.      */
  35.     private $orderRoute;
  36.     /**
  37.      * @var AbstractCustomerRoute
  38.      */
  39.     private $customerRoute;
  40.     /**
  41.      * @internal (flag:FEATURE_NEXT_14001) remove comment on feature release
  42.      */
  43.     private NewsletterAccountPageletLoader $newsletterAccountPageletLoader;
  44.     /**
  45.      * @internal
  46.      */
  47.     public function __construct(
  48.         GenericPageLoaderInterface $genericLoader,
  49.         EventDispatcherInterface $eventDispatcher,
  50.         AbstractOrderRoute $orderRoute,
  51.         AbstractCustomerRoute $customerRoute,
  52.         NewsletterAccountPageletLoader $newsletterAccountPageletLoader
  53.     ) {
  54.         $this->genericLoader $genericLoader;
  55.         $this->eventDispatcher $eventDispatcher;
  56.         $this->orderRoute $orderRoute;
  57.         $this->customerRoute $customerRoute;
  58.         $this->newsletterAccountPageletLoader $newsletterAccountPageletLoader;
  59.     }
  60.     /**
  61.      * @throws CategoryNotFoundException
  62.      * @throws CustomerNotLoggedInException
  63.      * @throws InconsistentCriteriaIdsException
  64.      * @throws MissingRequestParameterException
  65.      */
  66.     public function load(Request $requestSalesChannelContext $salesChannelContextCustomerEntity $customer): AccountOverviewPage
  67.     {
  68.         $page $this->genericLoader->load($request$salesChannelContext);
  69.         /** @var AccountOverviewPage $page */
  70.         $page AccountOverviewPage::createFrom($page);
  71.         $page->setCustomer($this->loadCustomer($salesChannelContext$customer));
  72.         if ($page->getMetaInformation()) {
  73.             $page->getMetaInformation()->setRobots('noindex,follow');
  74.         }
  75.         $order $this->loadNewestOrder($salesChannelContext$request);
  76.         if ($order !== null) {
  77.             $page->setNewestOrder($order);
  78.         }
  79.         if (Feature::isActive('FEATURE_NEXT_14001')) {
  80.             $newslAccountPagelet $this->newsletterAccountPageletLoader->load($request$salesChannelContext$customer);
  81.             $page->setNewsletterAccountPagelet($newslAccountPagelet);
  82.         }
  83.         $this->eventDispatcher->dispatch(
  84.             new AccountOverviewPageLoadedEvent($page$salesChannelContext$request)
  85.         );
  86.         return $page;
  87.     }
  88.     private function loadNewestOrder(SalesChannelContext $contextRequest $request): ?OrderEntity
  89.     {
  90.         $criteria = (new Criteria())
  91.             ->addSorting(new FieldSorting('orderDateTime'FieldSorting::DESCENDING))
  92.             ->addAssociation('lineItems')
  93.             ->addAssociation('lineItems.cover')
  94.             ->addAssociation('transactions.paymentMethod')
  95.             ->addAssociation('deliveries.shippingMethod')
  96.             ->addAssociation('addresses')
  97.             ->addAssociation('currency')
  98.             ->addAssociation('documents.documentType')
  99.             ->setLimit(1)
  100.             ->addAssociation('orderCustomer');
  101.         $criteria->getAssociation('transactions')
  102.             ->addSorting(new FieldSorting('createdAt'));
  103.         $apiRequest = new Request();
  104.         $event = new OrderRouteRequestEvent($request$apiRequest$context$criteria);
  105.         $this->eventDispatcher->dispatch($event);
  106.         $responseStruct $this->orderRoute
  107.             ->load($event->getStoreApiRequest(), $context$criteria);
  108.         return $responseStruct->getOrders()->first();
  109.     }
  110.     private function loadCustomer(SalesChannelContext $contextCustomerEntity $customer): CustomerEntity
  111.     {
  112.         $criteria = new Criteria();
  113.         $criteria->addAssociation('requestedGroup');
  114.         $criteria->addAssociation('defaultBillingAddress.country');
  115.         $criteria->addAssociation('defaultShippingAddress.country');
  116.         return $this->customerRoute->load(new Request(), $context$criteria$customer)->getCustomer();
  117.     }
  118. }