@@ -119,29 +119,23 @@ public function startOperation(
119119 );
120120 }
121121
122- Nexus::setCurrentContext (new NexusContext (
123- operation: self ::publicOperationContext ($ operationContext ),
124- workflowClient: $ workflowClient ,
125- current: $ contextWithServiceDefinition ,
126- startDetails: $ details ,
127- outboundPipeline: $ this ->interceptorProvider
128- ->getPipeline (NexusOperationOutboundCallsInterceptor::class),
129- ));
130- try {
131- $ result = $ this ->interceptorProvider
132- ->getPipeline (NexusOperationInboundCallsInterceptor::class)
133- ->with (
134- static fn (StartOperationInput $ input ): OperationStartResult => $ handler ->start (
135- $ input ->operationContext ,
136- $ input ->startDetails ,
137- $ input ->input ,
138- ),
139- /** @see NexusOperationInboundCallsInterceptor::startOperation() */
140- 'startOperation ' ,
141- )(new StartOperationInput ($ contextWithServiceDefinition , $ details , $ inputObject ));
142- } finally {
143- Nexus::setCurrentContext (null );
144- }
122+ $ result = $ this ->dispatch (
123+ new NexusContext (
124+ operation: self ::publicOperationContext ($ operationContext ),
125+ workflowClient: $ workflowClient ,
126+ current: $ contextWithServiceDefinition ,
127+ startDetails: $ details ,
128+ outboundPipeline: $ this ->interceptorProvider
129+ ->getPipeline (NexusOperationOutboundCallsInterceptor::class),
130+ ),
131+ static fn (StartOperationInput $ input ): OperationStartResult => $ handler ->start (
132+ $ input ->operationContext ,
133+ $ input ->startDetails ,
134+ $ input ->input ,
135+ ),
136+ 'startOperation ' ,
137+ new StartOperationInput ($ contextWithServiceDefinition , $ details , $ inputObject ),
138+ );
145139
146140 \assert ($ result instanceof OperationStartResult);
147141
@@ -180,27 +174,21 @@ public function cancelOperation(
180174
181175 $ contextWithServiceDefinition = $ context ->withServiceDefinition ($ instance ->prototype );
182176
183- Nexus::setCurrentContext (new NexusContext (
184- operation: self ::publicOperationContext ($ operationContext ),
185- workflowClient: $ workflowClient ,
186- current: $ contextWithServiceDefinition ,
187- cancelDetails: $ details ,
188- outboundPipeline: $ this ->interceptorProvider
189- ->getPipeline (NexusOperationOutboundCallsInterceptor::class),
190- ));
191- try {
192- $ this ->interceptorProvider
193- ->getPipeline (NexusOperationInboundCallsInterceptor::class)
194- ->with (
195- static function (CancelOperationInput $ input ) use ($ handler ): void {
196- $ handler ->cancel ($ input ->operationContext , $ input ->cancelDetails );
197- },
198- /** @see NexusOperationInboundCallsInterceptor::cancelOperation() */
199- 'cancelOperation ' ,
200- )(new CancelOperationInput ($ contextWithServiceDefinition , $ details ));
201- } finally {
202- Nexus::setCurrentContext (null );
203- }
177+ $ this ->dispatch (
178+ new NexusContext (
179+ operation: self ::publicOperationContext ($ operationContext ),
180+ workflowClient: $ workflowClient ,
181+ current: $ contextWithServiceDefinition ,
182+ cancelDetails: $ details ,
183+ outboundPipeline: $ this ->interceptorProvider
184+ ->getPipeline (NexusOperationOutboundCallsInterceptor::class),
185+ ),
186+ static function (CancelOperationInput $ input ) use ($ handler ): void {
187+ $ handler ->cancel ($ input ->operationContext , $ input ->cancelDetails );
188+ },
189+ 'cancelOperation ' ,
190+ new CancelOperationInput ($ contextWithServiceDefinition , $ details ),
191+ );
204192 }
205193
206194 private static function publicOperationContext (NexusOperationContext $ operationContext ): ?NexusOperationContext
@@ -211,6 +199,21 @@ private static function publicOperationContext(NexusOperationContext $operationC
211199 return $ operationContext ;
212200 }
213201
202+ /**
203+ * @param non-empty-string $method
204+ */
205+ private function dispatch (NexusContext $ dispatchContext , \Closure $ terminal , string $ method , object $ input ): mixed
206+ {
207+ Nexus::setCurrentContext ($ dispatchContext );
208+ try {
209+ return $ this ->interceptorProvider
210+ ->getPipeline (NexusOperationInboundCallsInterceptor::class)
211+ ->with ($ terminal , $ method )($ input );
212+ } finally {
213+ Nexus::setCurrentContext (null );
214+ }
215+ }
216+
214217 /**
215218 * @return array{NexusServiceInstance, OperationHandlerInterface}
216219 */
0 commit comments