|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Catalog\Model\ResourceModel\Product; |
| 20 | + |
| 21 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer as CategoryProductTableMaintainer; |
| 22 | +use Magento\Framework\App\ResourceConnection; |
| 23 | +use Magento\Store\Model\StoreManagerInterface; |
| 24 | + |
| 25 | +class GetCategories |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param ResourceConnection $resource |
| 29 | + * @param CategoryProductTableMaintainer $categoryProductTableMaintainer |
| 30 | + * @param StoreManagerInterface $storeManager |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + private readonly ResourceConnection $resource, |
| 34 | + private readonly CategoryProductTableMaintainer $categoryProductTableMaintainer, |
| 35 | + private readonly StoreManagerInterface $storeManager |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns list of categories ids for provided products |
| 41 | + * |
| 42 | + * @param int[] $productList |
| 43 | + * @return int[] |
| 44 | + */ |
| 45 | + public function execute(array $productList): array |
| 46 | + { |
| 47 | + $connection = $this->resource->getConnection(); |
| 48 | + $categories = []; |
| 49 | + foreach ($this->storeManager->getStores() as $store) { |
| 50 | + $select = $connection->select()->from( |
| 51 | + ['category_product_index' => $this->categoryProductTableMaintainer->getMainTable((int)$store->getId())], |
| 52 | + ['category_product_index.category_id'] |
| 53 | + ); |
| 54 | + $select->where('category_product_index.product_id IN (?)', $productList, \Zend_Db::INT_TYPE); |
| 55 | + $select->distinct(true); |
| 56 | + |
| 57 | + $categories += array_fill_keys($connection->fetchCol($select), true); |
| 58 | + } |
| 59 | + |
| 60 | + return array_keys($categories); |
| 61 | + } |
| 62 | +} |
0 commit comments