-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: handle import by path array
- Loading branch information
1 parent
637cc20
commit ca94890
Showing
7 changed files
with
429 additions
and
23 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
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; | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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
67
Model/ResourceModel/GetCategoryParentIdByCategoryPathArray.php
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,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; | ||
} | ||
} |
Oops, something went wrong.