77use Exception ;
88use Loki \Components \Component \ComponentInterface ;
99use Loki \Components \Component \ComponentRegistry ;
10+ use Loki \Components \Util \Controller \ComponentUpdate ;
11+ use Loki \Components \Util \Controller \ComponentUpdateFactory ;
1012use Magento \Framework \App \Action \HttpGetActionInterface ;
1113use Magento \Framework \App \Action \HttpPostActionInterface ;
1214use 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 });
0 commit comments