Skip to content

Commit

Permalink
improve: handle import by path array
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloargentiero committed Jul 26, 2022
1 parent 637cc20 commit ca94890
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 23 deletions.
96 changes: 96 additions & 0 deletions Model/CreateCategoryIdByCategoriesPathArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\FdiCategory\Model;

use GhostUnicorns\FdiCategory\Model\ResourceModel\CreateCategoryWithoutId;
use GhostUnicorns\FdiCategory\Model\ResourceModel\GetCategoryParentIdByCategoryPathArray;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\StoreRepositoryInterface;

class CreateCategoryIdByCategoriesPathArray
{
/**
* @var CreateCategoryWithoutId
*/
private $createCategoryWithoutId;

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

/**
* @var GetCategoryParentIdByCategoryPathArray
*/
private $getCategoryParentIdByCategoryPathArray;

/**
* @var UpdateCategory
*/
private $updateCategory;

/**
* @var StoreRepositoryInterface
*/
private $storeRepository;

/**
* @param CreateCategoryWithoutId $createCategoryWithoutId
* @param CategoryRepositoryInterface $categoryRepository
* @param GetCategoryParentIdByCategoryPathArray $getCategoryParentIdByCategoryPathArray
* @param UpdateCategory $updateCategory
* @param StoreRepositoryInterface $storeRepository
*/
public function __construct(
CreateCategoryWithoutId $createCategoryWithoutId,
CategoryRepositoryInterface $categoryRepository,
GetCategoryParentIdByCategoryPathArray $getCategoryParentIdByCategoryPathArray,
UpdateCategory $updateCategory,
StoreRepositoryInterface $storeRepository
) {
$this->createCategoryWithoutId = $createCategoryWithoutId;
$this->categoryRepository = $categoryRepository;
$this->getCategoryParentIdByCategoryPathArray = $getCategoryParentIdByCategoryPathArray;
$this->updateCategory = $updateCategory;
$this->storeRepository = $storeRepository;
}

/**
* @param array $categoriesPathArray
* @param int $rootCategory
* @param array $categoryData
* @param array $attributesToIgnore
* @return int
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(
array $categoriesPathArray,
int $rootCategory,
array $categoryData = [],
array $attributesToIgnore = []
): int {
$parentId = $this->getCategoryParentIdByCategoryPathArray->execute($categoriesPathArray, $rootCategory);

$categoryId = $this->createCategoryWithoutId->execute();

$category = $this->categoryRepository->get($categoryId);

foreach ($categoryData as $storeCode => $data) {
$storeId = $this->storeRepository->getActiveStoreByCode($storeCode)->getId();
$data['parent_id'] = $parentId;
$data['store_id'] = $storeId;
$this->updateCategory->execute($category, $data, $attributesToIgnore);
}

return $parentId;
}
}
19 changes: 0 additions & 19 deletions Model/ResourceModel/CreateCategoryWithId.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,4 @@ private function insertCategoryEntity(int $entityId)
'children_count' => 0,
]);
}

/**
* @param int $entityId
*/
private function updateCategoryEntity(int $entityId)
{
$tableName = $this->resourceConnection->getTablePrefix() .
$this->resourceConnection->getTableName(self::TABLE_NAME);
$connection = $this->resourceConnection->getConnection();
$connection->insert($tableName, [
'entity_id' => $entityId,
'attribute_set_id' => 3,
'parent_id' => 1,
'path' => 1,
'position' => 1,
'level' => 1,
'children_count' => 0,
]);
}
}
102 changes: 102 additions & 0 deletions Model/ResourceModel/CreateCategoryWithoutId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\FdiCategory\Model\ResourceModel;

use Magento\Framework\App\ProductMetadata;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\App\ResourceConnection;

class CreateCategoryWithoutId
{
/** @var string */
const SEQUENCE_TABLE_NAME = 'sequence_catalog_category';

/** @var string */
const TABLE_NAME = 'catalog_category_entity';

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var ProductMetadataInterface
*/
private $productMetadata;

/**
* @param ResourceConnection $resourceConnection
* @param ProductMetadataInterface $productMetadata
*/
public function __construct(
ResourceConnection $resourceConnection,
ProductMetadataInterface $productMetadata
) {
$this->resourceConnection = $resourceConnection;
$this->productMetadata = $productMetadata;
}

/**
* @return int
*/
public function execute(): int
{
if ($this->productMetadata->getEdition() !== ProductMetadata::EDITION_NAME) {
$id = $this->insertCategorySequence();
$this->insertCategoryEntity($id);
} else {
$id = $this->insertCategoryEntity();
}

return (int)$id;
}

/**
* @return int
*/
private function insertCategorySequence(): int
{
$tableName = $this->resourceConnection->getTablePrefix()
. $this->resourceConnection->getTableName(self::SEQUENCE_TABLE_NAME);
$connection = $this->resourceConnection->getConnection();
$connection->insert($tableName, []);
return (int)$connection->lastInsertId();
}

/**
* @param int|null $entityId
* @return void
*/
private function insertCategoryEntity(int $entityId = null): int
{
$tableName = $this->resourceConnection->getTablePrefix()
. $this->resourceConnection->getTableName(self::TABLE_NAME);

$connection = $this->resourceConnection->getConnection();
$data = [
'attribute_set_id' => 3,
'parent_id' => 0,
'path' => '0/99999',
'position' => 0,
'level' => 0,
'children_count' => 0,
];

if ($entityId) {
$data['path'] = '0/' . $entityId;
$data['entity_id'] = $entityId;
if ($this->productMetadata->getEdition() !== ProductMetadata::EDITION_NAME) {
$data['row_id'] = $entityId;
}
}
$connection->insert($tableName, $data);

return $entityId ?: (int)$connection->lastInsertId();
}
}
58 changes: 58 additions & 0 deletions Model/ResourceModel/GetCategoryIdByCategoryPathArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\FdiCategory\Model\ResourceModel;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;

class GetCategoryIdByCategoryPathArray
{
/**
* @var CollectionFactory
*/
private $categoryCollectionFactory;

/**
* @param CollectionFactory $categoryCollectionFactory
*/
public function __construct(CollectionFactory $categoryCollectionFactory)
{
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
* @param array $categoriesPathArray
* @param int $rootCategory
* @return int
* @throws LocalizedException
*/
public function execute(array $categoriesPathArray, int $rootCategory): int
{
$categoryId = $rootCategory;
$level = 1;

foreach ($categoriesPathArray as $categoryName) {
$level++;
$collection = $this->categoryCollectionFactory->create()
->addAttributeToFilter('parent_id', $categoryId)
->addAttributeToFilter('name', $categoryName)
->addAttributeToFilter('level', $level)
->setPageSize(1);

if ($collection->getSize()) {
$categoryId = (int)$collection->getFirstItem()->getId();
continue;
}

throw new LocalizedException(__('Category not found with name: %1', $categoryName));
}

return $categoryId;
}
}
67 changes: 67 additions & 0 deletions Model/ResourceModel/GetCategoryParentIdByCategoryPathArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\FdiCategory\Model\ResourceModel;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;

class GetCategoryParentIdByCategoryPathArray
{
/**
* @var CollectionFactory
*/
private $categoryCollectionFactory;

/**
* @param CollectionFactory $categoryCollectionFactory
*/
public function __construct(CollectionFactory $categoryCollectionFactory)
{
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
* @param array $categoriesPathArray
* @param int $rootCategory
* @return int
* @throws LocalizedException
*/
public function execute(array $categoriesPathArray, int $rootCategory): int
{
$parentId = $rootCategory;

foreach ($categoriesPathArray as $index => $categoryName) {
$level = $index + 2;

if (count($categoriesPathArray) === ($index + 1)) {
return $parentId;
}

$collection = $this->categoryCollectionFactory
->create()
->addAttributeToFilter('parent_id', $parentId)
->addAttributeToFilter('level', $level)
->addAttributeToFilter('name', $categoryName)
->setPageSize(1);

if (!$collection->getSize()) {
throw new LocalizedException(__(
'Category not found with parent_id: %1 - level: %2 - name: %3',
$parentId,
$level,
$categoryName
));
}

$parentId = (int)$collection->getFirstItem()->getId();
}

return $parentId;
}
}
Loading

0 comments on commit ca94890

Please sign in to comment.