Skip to content

Commit 3198c1b

Browse files
committed
B2B-2469: Improve category children loading
1 parent 6703f60 commit 3198c1b

File tree

2 files changed

+25
-55
lines changed
  • app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree/Wrapper

2 files changed

+25
-55
lines changed

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Forgery
1919
/**
2020
* Most top node id in the tree structure.
2121
*/
22-
private const DUMMY_ROOT_ID = 0;
22+
private const TOP_NODE_ID = 0;
2323

2424
/**
2525
* Flat index of the tree.
@@ -49,30 +49,28 @@ public function __construct(Hydrator $hydrator)
4949
*/
5050
public function forge(Category $category): void
5151
{
52-
$pathElements = array_map(
53-
function ($element) {
54-
return (int)$element;
55-
},
56-
explode('/', $category->getPath())
57-
);
58-
if (!$this->hasNodeById(self::DUMMY_ROOT_ID)) {
59-
$this->indexById[self::DUMMY_ROOT_ID] = new Node(self::DUMMY_ROOT_ID, $this);
52+
if (!$this->hasNodeById(self::TOP_NODE_ID)) {
53+
$this->indexById[self::TOP_NODE_ID] = new Node(self::TOP_NODE_ID);
6054
}
61-
$parentId = self::DUMMY_ROOT_ID;
62-
foreach ($pathElements as $id) {
63-
if ($this->hasNodeById($parentId)) {
55+
$parentId = self::TOP_NODE_ID;
56+
array_map(
57+
function ($id) use (&$parentId, $category) {
58+
$id = (int)$id;
6459
if (!$this->hasNodeById($id)) {
65-
$this->indexById[$id] = new Node($id, $this);
60+
$this->indexById[$id] = new Node($id);
61+
}
62+
if ($this->hasNodeById($parentId)) {
6663
$this->getNodeById($parentId)->addChild($this->indexById[$id]);
6764
if ($category->getId() == $id) {
68-
$this->indexById[$id]->setModel($category);
65+
$this->indexById[$id]->setModelData(
66+
$this->hydrator->hydrateCategory($category)
67+
);
6968
}
7069
}
71-
} elseif (!$this->hasNodeById($id)) {
72-
$this->indexById[$id] = new Node($id, $this);
73-
}
74-
$parentId = $id;
75-
}
70+
$parentId = $id;
71+
},
72+
explode('/', $category->getPath())
73+
);
7674
}
7775

7876
/**
@@ -96,14 +94,4 @@ public function hasNodeById(int $id) : bool
9694
{
9795
return isset($this->indexById[$id]);
9896
}
99-
100-
/**
101-
* Get category hydrator.
102-
*
103-
* @return Hydrator
104-
*/
105-
public function getHydrator()
106-
{
107-
return $this->hydrator;
108-
}
10997
}

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

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,26 @@ class Node
2525
private $children = [];
2626

2727
/**
28-
* @var Category
28+
* @var array
2929
*/
30-
private $model;
31-
32-
/**
33-
* @var Forgery
34-
*/
35-
private $forgery;
30+
private $modelData;
3631

3732
/**
3833
* @param int $id
39-
* @param Forgery $forgery
4034
*/
41-
public function __construct(int $id, Forgery $forgery)
35+
public function __construct(int $id)
4236
{
4337
$this->id = $id;
44-
$this->forgery = $forgery;
4538
}
4639

4740
/**
48-
* Set category model for node.
41+
* Set category model data for node.
4942
*
50-
* @param Category $category
5143
* @return $this
5244
*/
53-
public function setModel(Category $category): self
45+
public function setModelData(?array $modelData): self
5446
{
55-
$this->model = $category;
47+
$this->modelData = $modelData;
5648
return $this;
5749
}
5850

@@ -78,16 +70,6 @@ public function getChildren(): array
7870
return $this->children;
7971
}
8072

81-
/**
82-
* Get category model for node.
83-
*
84-
* @return Category
85-
*/
86-
public function getModel(): Category
87-
{
88-
return $this->model;
89-
}
90-
9173
/**
9274
* Get node id.
9375
*
@@ -105,11 +87,11 @@ public function getId(): int
10587
*/
10688
public function renderArray(): ?array
10789
{
108-
if (!$this->model) {
90+
if (!$this->modelData) {
10991
return null;
11092
}
11193
return array_merge(
112-
$this->forgery->getHydrator()->hydrateCategory($this->model),
94+
$this->modelData,
11395
[
11496
'children' => array_filter(
11597
array_map(

0 commit comments

Comments
 (0)