Skip to content

Commit 46779ff

Browse files
committed
Convert update error into ComponentUpdate value object
1 parent f47bb5f commit 46779ff

3 files changed

Lines changed: 82 additions & 15 deletions

File tree

Controller/Index/Html.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Exception;
88
use Loki\Components\Component\ComponentInterface;
99
use Loki\Components\Component\ComponentRegistry;
10+
use Loki\Components\Util\Controller\ComponentUpdate;
11+
use Loki\Components\Util\Controller\ComponentUpdateFactory;
1012
use Magento\Framework\App\Action\HttpGetActionInterface;
1113
use Magento\Framework\App\Action\HttpPostActionInterface;
1214
use Magento\Framework\App\ResponseInterface;
@@ -41,7 +43,8 @@ public function __construct(
4143
private readonly Config $config,
4244
private readonly AppState $appState,
4345
private readonly LoggerInterface $logger,
44-
private readonly ComponentRegistry $componentRegistry
46+
private readonly ComponentRegistry $componentRegistry,
47+
private readonly ComponentUpdateFactory $componentUpdateFactory
4548
) {
4649
}
4750

@@ -51,17 +54,17 @@ public function execute(): ResultInterface|ResponseInterface
5154
$this->requestDataLoader->mergeRequestParams();
5255
$layout = $this->layoutLoader->load($data['handles']);
5356

54-
$updates = $this->enrichUpdates($data['updates'], $layout);
57+
$updates = $this->getComponentUpdates($data['updates'], $layout);
5558
$updates = $this->sortUpdates($updates);
5659

5760
foreach ($updates as $update) {
5861
try {
59-
$debugMessage = 'Component update: '.$update['block']->getNameInLayout();
62+
$debugMessage = 'Component update: '.$update->getBlock()->getNameInLayout();
6063
$this->logger->debug($debugMessage);
6164

6265
$this->repositoryDispatcher->dispatch(
63-
$update['component'],
64-
$update['update']
66+
$update->getComponent(),
67+
$update->getComponentData(),
6568
);
6669
} catch (NoBlockFoundException $exception) {
6770
$this->logger->critical($exception);
@@ -92,24 +95,39 @@ public function execute(): ResultInterface|ResponseInterface
9295
return $this->getHtmlResult($htmlParts);
9396
}
9497

95-
private function enrichUpdates(array $updates, LayoutInterface $layout): array
98+
/**
99+
* @param array $updates
100+
* @param LayoutInterface $layout
101+
*
102+
* @return ComponentUpdate[]
103+
*/
104+
private function getComponentUpdates(array $updates, LayoutInterface $layout): array
96105
{
106+
$componentUpdates = [];
97107
foreach ($updates as $updateIndex => $update) {
98-
$update['block'] = $this->getBlock($layout, $update['blockName']);
99-
$update['component'] = $this->componentRegistry->getComponentFromBlock($update['block']);
100-
$updates[$updateIndex] = $update;
108+
$block =$this->getBlock($layout, $update['blockName']);
109+
$componentUpdate = $this->componentUpdateFactory->create([
110+
'block' => $block,
111+
'component' => $this->componentRegistry->getComponentFromBlock($block),
112+
'componentData' => $update['update'],
113+
]);
114+
115+
$componentUpdates[$updateIndex] = $componentUpdate;
101116
}
102117

103-
return $updates;
118+
return $componentUpdates;
104119
}
105120

121+
/**
122+
* @param ComponentUpdate[] $updates
123+
*
124+
* @return ComponentUpdate[]
125+
*/
106126
private function sortUpdates(array $updates): array
107127
{
108-
usort($updates, function (array $a, array $b) {
109-
/** @var ComponentInterface $componentA */
110-
$componentA = $a['component'];
111-
/** @var ComponentInterface $componentB */
112-
$componentB = $b['component'];
128+
usort($updates, function (ComponentUpdate $a, ComponentUpdate $b) {
129+
$componentA = $a->getComponent();
130+
$componentB = $b->getComponent();
113131

114132
return $componentA->getRepository()?->getPriority() <=> $componentB->getRepository()?->getPriority();
115133
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Util\Controller;
4+
5+
use Loki\Components\Component\Component;
6+
use Magento\Framework\View\Element\AbstractBlock;
7+
8+
class ComponentUpdate
9+
{
10+
public function __construct(
11+
private readonly AbstractBlock $block,
12+
private readonly Component $component,
13+
private readonly mixed $componentData = null
14+
) {
15+
}
16+
17+
public function getBlock(): AbstractBlock
18+
{
19+
return $this->block;
20+
}
21+
22+
public function getComponent(): Component
23+
{
24+
return $this->component;
25+
}
26+
27+
public function getComponentData(): mixed
28+
{
29+
return $this->componentData;
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Util\Controller;
4+
5+
use Magento\Framework\ObjectManagerInterface;
6+
7+
class ComponentUpdateFactory
8+
{
9+
public function __construct(
10+
private readonly ObjectManagerInterface $objectManager
11+
) {
12+
}
13+
14+
public function create(array $data = []): ComponentUpdate
15+
{
16+
return $this->objectManager->create(ComponentUpdate::class, $data);
17+
}
18+
}

0 commit comments

Comments
 (0)