Skip to content

Commit

Permalink
Pending changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Feb 6, 2024
1 parent 77f6aab commit 6601a9f
Show file tree
Hide file tree
Showing 3 changed files with 28 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.11] - 6 February 2024
### Fixed
- Fix PHP errors

## [3.7.10] - 6 February 2024
### Fixed
- Better error handling in CategoryProvider #209 @thomas-kl1
Expand Down
46 changes: 23 additions & 23 deletions Util/CategoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,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 @@ -46,7 +46,7 @@ public function __construct(
$this->filterGroupBuilder = $filterGroupBuilder;
$this->storeManager = $storeManager;
}

/**
* @param int[] $categoryIds
* @return void
Expand All @@ -58,10 +58,10 @@ public function addCategoryIds(array $categoryIds)
if (empty($categoryIds)) {
return;
}

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

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

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

/**
* @return CategoryInterface[]
* @throws NoSuchEntityException
Expand All @@ -87,19 +87,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, static function (CategoryInterface $category) {
return $category->getIsActive();
});
}

/**
* @param ProductInterface $product
* @return CategoryInterface
Expand All @@ -114,19 +114,19 @@ public function getFirstByProduct(ProductInterface $product): CategoryInterface

$category = null;
while ($category === null && $productCategoryId = array_shift($productCategoryIds)) {
$subject->addCategoryIds([$productCategoryId]);
$this->addCategoryIds([$productCategoryId]);
if ($this->categoryIds) {
$category = $subject->getLoadedCategories()[$productCategoryId] ?? null;
$category = $this->getLoadedCategories()[$productCategoryId] ?? null;
}
}

if ($category instanceof CategoryInterface) {
return $category;
}

throw new NoSuchEntityException(__('Product "%1" has no categories', $product->getSku()));
}

/**
* @param ProductInterface $product
* @return CategoryInterface[]
Expand All @@ -138,12 +138,12 @@ public function getAllByProduct(ProductInterface $product): array
if (empty($productCategoryIds)) {
throw new NoSuchEntityException(__('Product "%1" has no categories', $product->getSku()));
}

$this->addCategoryIds($productCategoryIds);

return array_intersect_key($this->getLoadedCategories(), array_flip($productCategoryIds));
}

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

/** @var FilterGroup $rootCategoryFilterGroup */
$rootCategoryFilterGroup = $this->filterGroupBuilder->create();
$rootCategoryFilterGroup->setFilters([
Expand All @@ -170,17 +170,17 @@ private function loadCategoriesByIds(array $categoryIds): array
->setValue('1/' . $this->getRootCategoryId() . '/%')
->create(),
]);

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

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

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

/**
* @param array $categoryIds
* @return array
Expand All @@ -193,7 +193,7 @@ private function filterRootCategoryIdFromCategoryIds(array $categoryIds): array
return (int)$categoryId !== $rootCategoryId;
});
}

/**
* @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.10",
"version": "3.7.11",
"license": "OSL-3.0",
"type": "magento2-module",
"homepage": "https://www.yireo.com/software/magento-extensions/googletagmanager2",
Expand Down

0 comments on commit 6601a9f

Please sign in to comment.