Skip to content

Commit

Permalink
Fix warnings when a product has no categories
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Feb 3, 2024
1 parent 79e06df commit 25218ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.7.8] - 3 February 2024
### Fixed
- Fix warnings when a product has no categories

## [3.7.7] - 1 February 2024
### Fixed
- Fix DI compilation due to 3.7.6
Expand Down
60 changes: 37 additions & 23 deletions Util/CategoryProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Yireo\GoogleTagManager2\Util;

Expand All @@ -20,18 +21,18 @@ class CategoryProvider
* @var int[]
*/
private array $categoryIds = [];

/**
* @var CategoryInterface[]
*/
private array $loadedCategories = [];

private CategoryListInterface $categoryListRepository;
private FilterBuilder $filterBuilder;
private SearchCriteriaBuilder $searchCriteriaBuilder;
private FilterGroupBuilder $filterGroupBuilder;
private StoreManagerInterface $storeManager;

public function __construct(
CategoryListInterface $categoryListRepository,
FilterBuilder $filterBuilder,
Expand All @@ -45,21 +46,26 @@ public function __construct(
$this->filterGroupBuilder = $filterGroupBuilder;
$this->storeManager = $storeManager;
}

/**
* @param int[] $categoryIds
* @return void
* @throws NoSuchEntityException
*/
public function addCategoryIds(array $categoryIds)
{
if (empty($categoryIds)) {
return;
}

$rootCategoryId = $this->getRootCategoryId();
$categoryIds = array_filter($categoryIds, function($categoryId) use ($rootCategoryId) {
$categoryIds = array_filter($categoryIds, function ($categoryId) use ($rootCategoryId) {
return (int)$categoryId !== $rootCategoryId;
});

$this->categoryIds = array_unique(array_merge($this->categoryIds, $categoryIds));
}

/**
* @param int $categoryId
* @return CategoryInterface
Expand All @@ -72,10 +78,10 @@ public function getById(int $categoryId): CategoryInterface
return $category;
}
}

throw new NotUsingSetProductSkusException('Using getCategoryById() delivers no result');
}

/**
* @return CategoryInterface[]
* @throws NoSuchEntityException
Expand All @@ -85,19 +91,19 @@ public function getLoadedCategories(): array
if (empty($this->categoryIds)) {
throw new NotUsingSetProductSkusException('Using getCategories() before setCategoryIds()');
}

$loadCategoryIds = array_diff($this->categoryIds, array_keys($this->loadedCategories));
if (count($loadCategoryIds) > 0) {
foreach ($this->loadCategoriesByIds($loadCategoryIds) as $category) {
$this->loadedCategories[(int)$category->getId()] = $category;
}
}

return array_filter($this->loadedCategories, function(CategoryInterface $category) {
return array_filter($this->loadedCategories, function (CategoryInterface $category) {
return $category->getIsActive();
});
}

/**
* @param ProductInterface $product
* @return CategoryInterface
Expand All @@ -106,12 +112,16 @@ public function getLoadedCategories(): array
public function getFirstByProduct(ProductInterface $product): CategoryInterface
{
$productCategoryIds = $product->getCategoryIds();
if (empty($productCategoryIds)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}

$productCategoryId = array_shift($productCategoryIds);
$this->addCategoryIds([$productCategoryId]);

return $this->getLoadedCategories()[$productCategoryId];
}

/**
* @param ProductInterface $product
* @return CategoryInterface[]
Expand All @@ -120,16 +130,20 @@ public function getFirstByProduct(ProductInterface $product): CategoryInterface
public function getAllByProduct(ProductInterface $product): array
{
$productCategoryIds = $product->getCategoryIds();
if (empty($productCategoryIds)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}

$this->addCategoryIds($productCategoryIds);

return array_filter(
$this->getLoadedCategories(),
function (CategoryInterface $category) use ($productCategoryIds) {
return in_array($category->getId(), $productCategoryIds);
}
);
}

/**
* @param array $categoryIds
* @return CategoryInterface[]
Expand All @@ -146,27 +160,27 @@ private function loadCategoriesByIds(array $categoryIds): array
->setValue($categoryIds)
->create(),
]);

/** @var FilterGroup $rootCategoryFilterGroup */
$rootCategoryFilterGroup = $this->filterGroupBuilder->create();
$rootCategoryFilterGroup->setFilters([
$this->filterBuilder
->setField('path')
->setConditionType('like')
->setValue('1/'.$this->getRootCategoryId().'/%')
->setValue('1/' . $this->getRootCategoryId() . '/%')
->create(),
]);

$this->searchCriteriaBuilder->setFilterGroups([
$entityIdFilterGroup,
$rootCategoryFilterGroup,
]);

$searchCriteria = $this->searchCriteriaBuilder->create();

return $this->categoryListRepository->getList($searchCriteria)->getItems();
}

/**
* @return int
* @throws NoSuchEntityException
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yireo/magento2-googletagmanager2",
"version": "3.7.7",
"version": "3.7.8",
"license": "OSL-3.0",
"type": "magento2-module",
"homepage": "https://www.yireo.com/software/magento-extensions/googletagmanager2",
Expand Down

0 comments on commit 25218ed

Please sign in to comment.