-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bf3fed
commit 2d9b793
Showing
7 changed files
with
279 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Yireo\GoogleTagManager2\Test\Functional\Util; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\App\ObjectManager; | ||
use PHPUnit\Framework\TestCase; | ||
use Yireo\GoogleTagManager2\Exception\NotUsingSetProductSkusException; | ||
use Yireo\GoogleTagManager2\Util\CategoryProvider; | ||
|
||
class CategoryProviderTest extends TestCase | ||
{ | ||
public function testGetCategoriesWithException() | ||
{ | ||
$this->expectException(NotUsingSetProductSkusException::class); | ||
$categoryProvider = ObjectManager::getInstance()->get(CategoryProvider::class); | ||
$categoryProvider->getLoadedCategories(); | ||
} | ||
|
||
public function testGetById() | ||
{ | ||
$categoryProvider = ObjectManager::getInstance()->get(CategoryProvider::class); | ||
$categoryProvider->addCategoryIds([11, 38]); | ||
$category = $categoryProvider->getById(11); | ||
$this->assertEquals(11, $category->getId()); | ||
} | ||
|
||
public function testGetCategories() | ||
{ | ||
$categoryProvider = ObjectManager::getInstance()->get(CategoryProvider::class); | ||
$categoryProvider->addCategoryIds([11, 38]); | ||
$categories = $categoryProvider->getLoadedCategories(); | ||
$this->assertEquals(2, count($categories)); | ||
|
||
$categoryProvider->addCategoryIds(['12', 14]); | ||
$categories = $categoryProvider->getLoadedCategories(); | ||
$this->assertEquals(4, count($categories)); | ||
|
||
$categoryProvider->addCategoryIds([12, 14]); | ||
$categories = $categoryProvider->getLoadedCategories(); | ||
$this->assertEquals(4, count($categories)); | ||
} | ||
|
||
public function testGetByProduct() | ||
{ | ||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); | ||
$product = $productRepository->getById(1); | ||
|
||
$categoryProvider = ObjectManager::getInstance()->get(CategoryProvider::class); | ||
$categoryProvider->addCategoryIds([11, 38]); | ||
$categories = $categoryProvider->getAllByProduct($product); | ||
$this->assertEquals(2, count($categories)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\GoogleTagManager2\Util; | ||
|
||
use Magento\Catalog\Api\CategoryListInterface; | ||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Framework\Api\FilterBuilder; | ||
use Magento\Framework\Api\Search\FilterGroup; | ||
use Magento\Framework\Api\Search\FilterGroupBuilder; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Store\Model\Store; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Yireo\GoogleTagManager2\Exception\NotUsingSetProductSkusException; | ||
|
||
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, | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
FilterGroupBuilder $filterGroupBuilder, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->categoryListRepository = $categoryListRepository; | ||
$this->filterBuilder = $filterBuilder; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
$this->filterGroupBuilder = $filterGroupBuilder; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @param int[] $categoryIds | ||
* @return void | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function addCategoryIds(array $categoryIds) | ||
{ | ||
$rootCategoryId = $this->getRootCategoryId(); | ||
$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 | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function getById(int $categoryId): CategoryInterface | ||
{ | ||
foreach ($this->getLoadedCategories() as $category) { | ||
if ((int)$category->getId() === $categoryId) { | ||
return $category; | ||
} | ||
} | ||
|
||
throw new NotUsingSetProductSkusException('Using getCategoryById() delivers no result'); | ||
} | ||
|
||
/** | ||
* @return CategoryInterface[] | ||
* @throws NoSuchEntityException | ||
*/ | ||
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 $category->getIsActive(); | ||
}); | ||
} | ||
|
||
/** | ||
* @param ProductInterface $product | ||
* @return CategoryInterface | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function getFirstByProduct(ProductInterface $product): CategoryInterface | ||
{ | ||
$productCategoryIds = $product->getCategoryIds(); | ||
$productCategoryId = array_shift($productCategoryIds); | ||
$this->addCategoryIds([$productCategoryId]); | ||
|
||
return $this->getLoadedCategories()[$productCategoryId]; | ||
} | ||
|
||
/** | ||
* @param ProductInterface $product | ||
* @return CategoryInterface[] | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function getAllByProduct(ProductInterface $product): array | ||
{ | ||
$productCategoryIds = $product->getCategoryIds(); | ||
$this->addCategoryIds($productCategoryIds); | ||
|
||
return array_filter( | ||
$this->getLoadedCategories(), | ||
function (CategoryInterface $category) use ($productCategoryIds) { | ||
return in_array($category->getId(), $productCategoryIds); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param array $categoryIds | ||
* @return CategoryInterface[] | ||
* @throws NoSuchEntityException | ||
*/ | ||
private function loadCategoriesByIds(array $categoryIds): array | ||
{ | ||
/** @var FilterGroup $entityIdFilterGroup */ | ||
$entityIdFilterGroup = $this->filterGroupBuilder->create(); | ||
$entityIdFilterGroup->setFilters([ | ||
$this->filterBuilder | ||
->setField('entity_id') | ||
->setConditionType('in') | ||
->setValue($categoryIds) | ||
->create(), | ||
]); | ||
|
||
/** @var FilterGroup $rootCategoryFilterGroup */ | ||
$rootCategoryFilterGroup = $this->filterGroupBuilder->create(); | ||
$rootCategoryFilterGroup->setFilters([ | ||
$this->filterBuilder | ||
->setField('path') | ||
->setConditionType('like') | ||
->setValue('1/'.$this->getRootCategoryId().'/%') | ||
->create(), | ||
]); | ||
|
||
$this->searchCriteriaBuilder->setFilterGroups([ | ||
$entityIdFilterGroup, | ||
$rootCategoryFilterGroup, | ||
]); | ||
|
||
$searchCriteria = $this->searchCriteriaBuilder->create(); | ||
|
||
return $this->categoryListRepository->getList($searchCriteria)->getItems(); | ||
} | ||
|
||
/** | ||
* @return int | ||
* @throws NoSuchEntityException | ||
*/ | ||
private function getRootCategoryId(): int | ||
{ | ||
/** @var Store $store */ | ||
$store = $this->storeManager->getStore(); | ||
return (int)$store->getRootCategoryId(); | ||
} | ||
} |
Oops, something went wrong.