* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShop\PrestaShop\Adapter\Manufacturer; use Manufacturer; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchProviderInterface; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult; use PrestaShop\PrestaShop\Core\Product\Search\SortOrderFactory; use Symfony\Contracts\Translation\TranslatorInterface; class ManufacturerProductSearchProvider implements ProductSearchProviderInterface { private $translator; private $manufacturer; private $sortOrderFactory; public function __construct( TranslatorInterface $translator, Manufacturer $manufacturer ) { $this->translator = $translator; $this->manufacturer = $manufacturer; $this->sortOrderFactory = new SortOrderFactory($this->translator); } /** * @param ProductSearchContext $context * @param ProductSearchQuery $query * @param string $type * * @return array|int */ private function getProductsOrCount( ProductSearchContext $context, ProductSearchQuery $query, $type = 'products' ) { $result = $this->manufacturer->getProducts( $this->manufacturer->id, $context->getIdLang(), $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay(), $type !== 'products' ); return $type !== 'products' ? (int) $result : $result; } /** * @param ProductSearchContext $context * @param ProductSearchQuery $query * * @return ProductSearchResult */ public function runQuery( ProductSearchContext $context, ProductSearchQuery $query ) { $products = $this->getProductsOrCount($context, $query, 'products'); $count = $this->getProductsOrCount($context, $query, 'count'); $result = new ProductSearchResult(); if (!empty($products)) { $result ->setProducts($products) ->setTotalProductsCount($count); $result->setAvailableSortOrders( $this->sortOrderFactory->getDefaultSortOrders() ); } return $result; } }