|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\DownloadableGraphQl\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Downloadable\Api\Data\LinkInterface; |
| 12 | +use Magento\Downloadable\Model\ResourceModel\Link\Collection; |
| 13 | +use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * Returns links of a particular downloadable product |
| 17 | + */ |
| 18 | +class GetDownloadableProductLinks |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CollectionFactory |
| 22 | + */ |
| 23 | + private $linkCollectionFactory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param CollectionFactory $linkCollectionFactory |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + CollectionFactory $linkCollectionFactory |
| 30 | + ) { |
| 31 | + $this->linkCollectionFactory = $linkCollectionFactory; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns downloadable product links |
| 36 | + * |
| 37 | + * @param Product $product |
| 38 | + * @param array $selectedLinksIds |
| 39 | + * @return LinkInterface[] |
| 40 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 41 | + */ |
| 42 | + public function execute(Product $product, array $selectedLinksIds = []): array |
| 43 | + { |
| 44 | + /** @var Collection */ |
| 45 | + $links = $this->linkCollectionFactory->create(); |
| 46 | + $links->addTitleToResult($product->getStoreId()) |
| 47 | + ->addPriceToResult($product->getStore()->getWebsiteId()) |
| 48 | + ->addProductToFilter($product->getId()); |
| 49 | + |
| 50 | + if (count($selectedLinksIds) > 0) { |
| 51 | + $links->addFieldToFilter('main_table.link_id', ['in' => $selectedLinksIds]); |
| 52 | + } |
| 53 | + return $links->getItems(); |
| 54 | + } |
| 55 | +} |
0 commit comments