Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init category indexer #858

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
adb846e
Init category indexer
supercid Jun 24, 2024
95324ee
Configure ModeSwithcers, Refinement CategoryIndexer Class, preparatio…
ugljesaspx Aug 28, 2024
ced4270
Category indexer
ugljesaspx Sep 3, 2024
34b8961
Fixing unused classes, and change names
ugljesaspx Sep 16, 2024
78c5b0e
Merge branch 'develop' into feature/category-indexer
supercid Sep 17, 2024
3ce60f6
Fix warning errors
ugljesaspx Sep 17, 2024
3de71b3
Fix warnings and errors
ugljesaspx Sep 17, 2024
ce95047
Merge branch 'develop' into feature/category-indexer
supercid Sep 18, 2024
a65c321
Fixing unused classes, and change names
ugljesaspx Sep 18, 2024
9dfe7a9
Merge branch 'feature/category-indexer' of github.com:Nosto/nosto-mag…
ugljesaspx Sep 18, 2024
e7bafbf
Fixing unused classes, and change names, test parent category ids fnc
ugljesaspx Sep 18, 2024
8537de3
Update Model/Category/Repository.php
supercid Sep 30, 2024
22444cf
Update Model/Category/Repository.php
supercid Sep 30, 2024
dac61dc
Update Model/Indexer/Dimensions/ModeSwitcher/ModeSwitcherConfiguratio…
supercid Sep 30, 2024
7d9b919
Update Model/Service/Update/CategoryUpdateService.php
supercid Sep 30, 2024
d06c586
Category connection with Queue
ugljesaspx Nov 1, 2024
ccaf9ac
Merge branch 'feature/category-indexer' of github.com:Nosto/nosto-mag…
ugljesaspx Nov 1, 2024
7a2d8ee
Adapat code for category reindex
ugljesaspx Nov 1, 2024
ee3b5b5
Adapat code for category reindex
ugljesaspx Nov 1, 2024
7e75e9e
Merge branch 'develop' into feature/category-indexer
ugljesaspx Dec 17, 2024
c04cd74
Prepare for send categories to Nosto
ugljesaspx Dec 19, 2024
ee349e7
implement category sending to nosto
makso8makso Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Exception/ParentCategoryDisabledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <contact@nosto.com>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Exception;

use Nosto\NostoException;
use Throwable;

class ParentCategoryDisabledException extends NostoException
{
public function __construct($categoryId, $code = 0, Throwable $previous = null)
{
$message = "Parent category is disabled for category with id: " . $categoryId;
parent::__construct($message, $code, $previous);
}
}
7 changes: 0 additions & 7 deletions Model/Category/CollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
use Magento\Catalog\Model\ResourceModel\Category\Collection as MagentoCategoryCollection;
use Nosto\Tagging\Model\Category\Builder as NostoCategoryBuilder;
use Nosto\Tagging\Model\ResourceModel\Magento\Category\CollectionBuilder as CategoryCollectionBuilder;
use Nosto\Tagging\Model\Service\Product\Category\CategoryServiceInterface;
use Traversable;

/**
Expand All @@ -59,28 +58,22 @@ class CollectionBuilder
/** @var CategoryCollectionBuilder */
private CategoryCollectionBuilder $categoryCollectionBuilder;

/** @var CategoryServiceInterface */
private CategoryServiceInterface $categoryService;

/** @var NostoLogger */
private NostoLogger $logger;

/**
* Collection constructor.
* @param NostoCategoryBuilder $categoryBuilder
* @param CategoryCollectionBuilder $categoryCollectionBuilder
* @param CategoryServiceInterface $categoryService
* @param NostoLogger $logger
*/
public function __construct(
NostoCategoryBuilder $categoryBuilder,
CategoryCollectionBuilder $categoryCollectionBuilder,
CategoryServiceInterface $categoryService,
NostoLogger $logger
) {
$this->categoryBuilder = $categoryBuilder;
$this->categoryCollectionBuilder = $categoryCollectionBuilder;
$this->categoryService = $categoryService;
$this->logger = $logger;
}

Expand Down
189 changes: 189 additions & 0 deletions Model/Category/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <contact@nosto.com>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Category;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\Store;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Nosto\Tagging\Exception\ParentCategoryDisabledException;

/**
* Repository wrapper class for fetching categories
*/
class Repository
{
private array $parentCategoryIdCache = [];

/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
private SearchCriteriaBuilder $searchCriteriaBuilder;

/** @var CategoryRepository $categoryRepository */
private CategoryRepository $categoryRepository;

/** @var CategoryCollectionFactory $categoryCollectionFactory */
private CategoryCollectionFactory $categoryCollectionFactory;

/**
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
CategoryRepository $categoryRepository,
CategoryCollectionFactory $categoryCollectionFactory
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->categoryRepository = $categoryRepository;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
* Gets categories by category ids
*
* @param array $ids
*/
public function getByIds(array $ids)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this method if it is not being used

{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('entity_id', $ids, 'in')
->create();
return $this->categoryRepository->getList($searchCriteria);

Check failure on line 87 in Model/Category/Repository.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to undeclared method \Magento\Catalog\Model\CategoryRepository::getList
}

/**
* Gets the parent category ID's for a given category
*
* @param CategoryInterface $category
* @return string[]|null
*
* @throws ParentCategoryDisabledException
*/
public function resolveParentCategoryIds(CategoryInterface $category): ?array
{
if ($this->getParentIdsFromCache($category)) {
return $this->getParentIdsFromCache($category);
}

if ($category->getLevel() < 1 && !$category->getIsActive()) {
throw new ParentCategoryDisabledException(
sprintf(
'Category with id %s is disabled',
$category->getId()
)
);
}

$parentCategoryIds = null;
if ($category->getLevel() >= 1) {
$parentCategoryIds = $this->getCategoryParentIds($category->getPath());
$this->saveParentIdsToCache($category, $parentCategoryIds);
}

return $parentCategoryIds;
}

/**
* @param Store $store
* @param array $categoryIds
*
* @return CategoryCollection
* @throws LocalizedException
*/
public function getCategoryCollectionQuery(Store $store, array $categoryIds = [])
{
$categories = $this->categoryCollectionFactory->create()
->distinct(true)

Check warning on line 132 in Model/Category/Repository.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Possible slow SQL method distinct detected
->addNameToResult()
->setStoreId($store->getId())
->addUrlRewriteToResult()
->addAttributeToFilter('level', ['gt' => 1])
->addAttributeToSelect(array_merge(['name', 'is_active', 'include_in_menu']))
->addOrderField('entity_id');

if ($categoryIds) {
$categories->addAttributeToFilter('entity_id', ['in' => $categoryIds]);
}

return $categories;
}

/**
* Get Parent Category IDS
*
* @param $path
* @return array
*/
private function getCategoryParentIds($path): array
{
$parentCategories = explode('/', $path);

array_pop($parentCategories);

return $parentCategories;
}

/**
* Get parent ids from cache. Return null if the cache is not available
*
* @param CategoryInterface $category
* @return string[]|null
*/
private function getParentIdsFromCache(CategoryInterface $category)
{
if (isset($this->parentCategoryIdCache[$category->getId()])) {
return $this->parentCategoryIdCache[$category->getId()];
}

return null;
}

/**
* Saves the parents category ids to internal cache to avoid redundant
* database queries
*
* @param CategoryInterface $category
* @param string[] $parentCategoryIds
*/
private function saveParentIdsToCache(CategoryInterface $category, array $parentCategoryIds)
{
$this->parentCategoryIdCache[$category->getId()] = $parentCategoryIds;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}

Check warning on line 189 in Model/Category/Repository.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

The closing brace for the class must go on the next line after the body
2 changes: 1 addition & 1 deletion Model/Indexer/AbstractIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
use Nosto\Tagging\Helper\Scope as NostoHelperScope;
use Nosto\Tagging\Logger\Logger as NostoLogger;
use Nosto\Tagging\Model\Indexer\Dimensions\AbstractDimensionModeConfiguration as DimensionModeConfiguration;
use Nosto\Tagging\Model\Indexer\Dimensions\ModeSwitcherInterface;
use Nosto\Tagging\Model\Indexer\Dimensions\ModeSwitcher\ModeSwitcherInterface;
use Nosto\Tagging\Model\Indexer\Dimensions\StoreDimensionProvider;
use Nosto\Tagging\Model\Service\Indexer\IndexerStatusServiceInterface;
use Nosto\Tagging\Util\Benchmark;
Expand Down
Loading
Loading