Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/SearchAutocomplete/Index/Magento/Catalog/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/old/app/code/Cnc/SearchAutocomplete/Index/Magento/Catalog/CategoryInstantProvider.php
<?php
/**
 * Copyright (c) 2019 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * cnc_searchautocomplete_m2
 * <[email protected]>
 */
declare(strict_types=1);

namespace Cnc\SearchAutocomplete\Index\Magento\Catalog;

use Cnc\Catalog\Model\Config;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Store\Model\StoreManagerInterface;
use Mirasvit\Search\Index\Magento\Catalog\Category\InstantProvider;
use Magento\Catalog\Api\Data\CategoryInterface;
use Mirasvit\Search\Service\IndexService;

class CategoryInstantProvider extends InstantProvider
{
    private $categoryRepository;

    private $storeManager;

    /**
     * CategoryInstantProvider constructor.
     * @param CategoryRepository $categoryRepository
     * @param StoreManagerInterface $storeManager
     * @param IndexService $indexService
     */
    public function __construct(
        CategoryRepository $categoryRepository,
        StoreManagerInterface $storeManager,
        IndexService $indexService,
        \Magento\Framework\App\ResourceConnection $resource,
        \Mirasvit\Search\Service\MapperService $mapperService,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        parent::__construct($resource, $categoryFactory, $storeManager, $indexService, $mapperService);
        $this->categoryRepository = $categoryRepository;
        $this->storeManager = $storeManager;
    }

    /**
     * @param int $storeId
     * @param int $limit
     * @return array
     */
    public function getItems(int $storeId, int $limit, int $page = 0): array
    {
        $items = [];

        /** @var \Magento\Catalog\Model\Category $category */
        foreach ($this->getCollection($limit) as $category) {
            $items[] = $this->mapCategory($category, $storeId);
        }

        return $items;
    }

    /**
     * @param \Magento\Catalog\Model\Category $category
     * @param int $storeId
     *
     * @return array
     */
    private function mapCategory($category, int $storeId): array
    {
        $category = $category->setStoreId($storeId);
        $category = $category->load($category->getId());

        // @override START
        $iconCssClass = $category->getData(Config::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS);
        if (!$iconCssClass) {
            $iconCssClass = $category->getParentCategory()->getData(Config::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS);
        }

        return [
            'name' => $category->getName(),
            'url' => $category->getUrl(),
            'icon_css_class' => $iconCssClass ? 'icon-' . $iconCssClass : null
        ];
        // @override END
    }

    private function getFullPath(CategoryInterface $category, int $storeId): string
    {
        $store = $this->storeManager->getStore($storeId);
        $rootId = $store->getRootCategoryId();

        $result = [
            $category->getName(),
        ];

        do {
            if (!$category->getParentId()) {
                break;
            }
            $category = $this->categoryRepository->get($category->getParentId());
            $category = $category->setStoreId($storeId);
            $category->load($category->getId());

            if (!$category->getIsActive() && $category->getId() != $rootId) {
                break;
            }

            if ($category->getId() != $rootId) {
                $result[] = $category->getName();
            }
        } while ($category->getId() != $rootId);

        $result = array_reverse($result);

        return implode('<i>›</i>', $result);
    }
}

Spamworldpro Mini