Skip to content

Commit

Permalink
Merge pull request #43 from tddwizard/category-builder-improvements
Browse files Browse the repository at this point in the history
Save category with global scope via resource model
  • Loading branch information
schmengler authored May 8, 2020
2 parents e1936c1 + 093dfe9 commit 32fb867
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Catalog/CategoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use \Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\CategoryProductLinkInterface;
use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory;
Expand All @@ -21,6 +23,11 @@ class CategoryBuilder
*/
private $categoryRepository;

/**
* @var CategoryResource
*/
private $categoryResource;

/**
* @var CategoryLinkRepositoryInterface
*/
Expand All @@ -38,12 +45,14 @@ class CategoryBuilder

public function __construct(
CategoryRepositoryInterface $categoryRepository,
CategoryResource $categoryResource,
CategoryLinkRepositoryInterface $categoryLinkRepository,
CategoryProductLinkInterfaceFactory $productLinkFactory,
CategoryInterface $category,
array $skus
) {
$this->categoryRepository = $categoryRepository;
$this->categoryResource = $categoryResource;
$this->categoryLinkRepository = $categoryLinkRepository;
$this->category = $category;
$this->skus = $skus;
Expand All @@ -60,9 +69,11 @@ public static function topLevelCategory(ObjectManagerInterface $objectManager =

$category->setName('Top Level Category');
$category->setIsActive(true);
$category->setPath('1/2');

return new self(
$objectManager->create(CategoryRepositoryInterface::class),
$objectManager->create(CategoryResource::class),
$objectManager->create(CategoryLinkRepositoryInterface::class),
$objectManager->create(CategoryProductLinkInterfaceFactory::class),
$category,
Expand All @@ -83,10 +94,11 @@ public static function childCategoryOf(

$category->setName('Child Category');
$category->setIsActive(true);
$category->setParentId($parent->getId());
$category->setPath($parent->getCategory()->getPath());

return new self(
$objectManager->create(CategoryRepositoryInterface::class),
$objectManager->create(CategoryResource::class),
$objectManager->create(CategoryLinkRepositoryInterface::class),
$objectManager->create(CategoryProductLinkInterfaceFactory::class),
$category,
Expand Down Expand Up @@ -147,16 +159,20 @@ public function build() : CategoryInterface
$builder->category->setData('url_key', sha1(uniqid('', true)));
}

$category = $builder->categoryRepository->save($builder->category);
// Save with global scope if not specified otherwise
if ($builder->category instanceof Category && !$builder->category->hasData('store_id')) {
$builder->category->setStoreId(0);
}
$builder->categoryResource->save($builder->category);

foreach ($builder->skus as $position => $sku) {
/** @var CategoryProductLinkInterface $productLink */
$productLink = $builder->productLinkFactory->create();
$productLink->setSku($sku);
$productLink->setPosition($position);
$productLink->setCategoryId($category->getId());
$productLink->setCategoryId($builder->category->getId());
$builder->categoryLinkRepository->save($productLink);
}
return $category;
return $builder->category;
}
}
8 changes: 8 additions & 0 deletions src/Catalog/CategoryFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class CategoryFixture
*/
private $category;

/**
* @return CategoryInterface
*/
public function getCategory(): CategoryInterface
{
return $this->category;
}

public function __construct(CategoryInterface $category)
{
$this->category = $category;
Expand Down

0 comments on commit 32fb867

Please sign in to comment.