Skip to content

Commit 13188f3

Browse files
committed
B2B-2469: Improve category children loading
1 parent 29b215a commit 13188f3

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree/Wrapper/Node.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree\Wrapper;
99

10-
use Magento\Catalog\Model\Category;
11-
1210
/**
1311
* Category tree node wrapper.
1412
*/
@@ -39,7 +37,7 @@ public function __construct(int $id)
3937

4038
/**
4139
* Set category model data for node.
42-
*
40+
*
4341
* @param array|null $modelData
4442
*
4543
* @return $this

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree/Wrapper/Forgery.php renamed to app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree/Wrapper/NodeWrapper.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Tree node forgery for category tree wrapper.
1616
*/
17-
class Forgery
17+
class NodeWrapper
1818
{
1919
/**
2020
* Most top node id in the tree structure.
@@ -26,7 +26,7 @@ class Forgery
2626
*
2727
* @var array
2828
*/
29-
private array $indexById = [];
29+
private array $index = [];
3030

3131
/**
3232
* @var Hydrator
@@ -47,25 +47,23 @@ public function __construct(Hydrator $hydrator)
4747
* @param Category $category
4848
* @return void
4949
*/
50-
public function forge(Category $category): void
50+
public function wrap(Category $category): void
5151
{
52-
if (!$this->hasNodeById(self::TOP_NODE_ID)) {
53-
$this->indexById[self::TOP_NODE_ID] = new Node(self::TOP_NODE_ID);
52+
if (!$this->hasNode(self::TOP_NODE_ID)) {
53+
$this->index[self::TOP_NODE_ID] = new Node(self::TOP_NODE_ID);
5454
}
5555
$parentId = self::TOP_NODE_ID;
5656
array_map(
5757
function ($id) use (&$parentId, $category) {
5858
$id = (int)$id;
59-
if (!$this->hasNodeById($id)) {
60-
$this->indexById[$id] = new Node($id);
59+
if (!$this->hasNode($id)) {
60+
$this->index[$id] = new Node($id);
6161
}
62-
if ($this->hasNodeById($parentId)) {
63-
$this->getNodeById($parentId)->addChild($this->indexById[$id]);
64-
if ($category->getId() == $id) {
65-
$this->indexById[$id]->setModelData(
66-
$this->hydrator->hydrateCategory($category)
67-
);
68-
}
62+
$this->index[$parentId]->addChild($this->index[$id]);
63+
if ($category->getId() == $id) {
64+
$this->index[$id]->setModelData(
65+
$this->hydrator->hydrateCategory($category)
66+
);
6967
}
7068
$parentId = $id;
7169
},
@@ -79,9 +77,9 @@ function ($id) use (&$parentId, $category) {
7977
* @param int $id
8078
* @return Node|null
8179
*/
82-
public function getNodeById(int $id) : ?Node
80+
public function getNode(int $id) : ?Node
8381
{
84-
return $this->indexById[$id] ?? null;
82+
return $this->index[$id] ?? null;
8583
}
8684

8785
/**
@@ -90,8 +88,8 @@ public function getNodeById(int $id) : ?Node
9088
* @param int $id
9189
* @return bool
9290
*/
93-
public function hasNodeById(int $id) : bool
91+
public function hasNode(int $id) : bool
9492
{
95-
return isset($this->indexById[$id]);
93+
return isset($this->index[$id]);
9694
}
9795
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99

1010
use Magento\Catalog\Model\Category;
1111
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12-
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree\Wrapper\Forgery;
13-
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree\Wrapper\ForgeryFactory;
12+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree\Wrapper\NodeWrapperFactory;
1413

1514
/**
1615
* Data extractor for category tree processing in GraphQL resolvers.
1716
*/
1817
class ExtractDataFromCategoryTree
1918
{
2019
/**
21-
* @var ForgeryFactory
20+
* @var NodeWrapperFactory
2221
*/
23-
private $resultTreeForgeryFactory;
22+
private $nodeWrapperFactory;
2423

2524
/**
26-
* @param ForgeryFactory $resultTreeForgeryFactory
25+
* @param NodeWrapperFactory $nodeWrapperFactory
2726
*/
28-
public function __construct(ForgeryFactory $resultTreeForgeryFactory)
27+
public function __construct(NodeWrapperFactory $nodeWrapperFactory)
2928
{
30-
$this->resultTreeForgeryFactory = $resultTreeForgeryFactory;
29+
$this->nodeWrapperFactory = $nodeWrapperFactory;
3130
}
3231

3332
/**
@@ -39,15 +38,14 @@ public function __construct(ForgeryFactory $resultTreeForgeryFactory)
3938
*/
4039
public function buildTree(Collection $collection, array $topLevelCategories) : array
4140
{
42-
/** @var Forgery $forgery */
43-
$forgery = $this->resultTreeForgeryFactory->create();
41+
$wrapper = $this->nodeWrapperFactory->create();
4442
/** @var Category $item */
4543
foreach ($collection->getItems() as $item) {
46-
$forgery->forge($item);
44+
$wrapper->wrap($item);
4745
}
4846
$tree = [];
4947
foreach ($topLevelCategories as $topLevelCategory) {
50-
$tree[] = $forgery->getNodeById($topLevelCategory)->renderArray();
48+
$tree[] = $wrapper->getNode($topLevelCategory)->renderArray();
5149
}
5250
return $this->sortTree($tree);
5351
}

0 commit comments

Comments
 (0)