33namespace SolutionForest \WorkflowMastery \Core ;
44
55use Illuminate \Contracts \Events \Dispatcher as EventDispatcher ;
6- use Illuminate \Support \Facades \Log ;
76use SolutionForest \WorkflowMastery \Contracts \StorageAdapter ;
87use SolutionForest \WorkflowMastery \Events \WorkflowCancelled ;
98use SolutionForest \WorkflowMastery \Events \WorkflowStarted ;
109
1110class WorkflowEngine
1211{
13- private DefinitionParser $ parser ;
12+ private readonly DefinitionParser $ parser ;
1413
15- private StateManager $ stateManager ;
14+ private readonly StateManager $ stateManager ;
1615
17- private Executor $ executor ;
18-
19- private EventDispatcher $ eventDispatcher ;
20-
21- private StorageAdapter $ storage ;
16+ private readonly Executor $ executor ;
2217
2318 public function __construct (
24- StorageAdapter $ storage ,
25- ?EventDispatcher $ eventDispatcher = null
19+ private readonly StorageAdapter $ storage ,
20+ private readonly ?EventDispatcher $ eventDispatcher = null
2621 ) {
27- $ this ->storage = $ storage ;
2822 $ this ->parser = new DefinitionParser ;
2923 $ this ->stateManager = new StateManager ($ storage );
3024 $ this ->executor = new Executor ($ this ->stateManager );
31- $ this ->eventDispatcher = $ eventDispatcher ?? app (EventDispatcher::class);
25+
26+ // If no event dispatcher is provided, we'll use a fallback approach
27+ if ($ this ->eventDispatcher === null ) {
28+ // We'll handle this case in the methods that use the event dispatcher
29+ }
3230 }
3331
3432 /**
@@ -53,7 +51,7 @@ public function start(string $workflowId, array $definition, array $context = []
5351 $ this ->stateManager ->save ($ instance );
5452
5553 // Dispatch start event
56- $ this ->eventDispatcher -> dispatch (new WorkflowStarted (
54+ $ this ->dispatchEvent (new WorkflowStarted (
5755 $ instance ->getId (),
5856 $ instance ->getDefinition ()->getName (),
5957 $ context
@@ -107,7 +105,7 @@ public function cancel(string $instanceId, string $reason = ''): WorkflowInstanc
107105 $ this ->stateManager ->save ($ instance );
108106
109107 // Dispatch cancel event
110- $ this ->eventDispatcher -> dispatch (new WorkflowCancelled (
108+ $ this ->dispatchEvent (new WorkflowCancelled (
111109 $ instance ->getId (),
112110 $ instance ->getDefinition ()->getName (),
113111 $ reason
@@ -121,12 +119,7 @@ public function cancel(string $instanceId, string $reason = ''): WorkflowInstanc
121119 */
122120 public function getWorkflow (string $ workflowId ): WorkflowInstance
123121 {
124- $ instance = $ this ->stateManager ->load ($ workflowId );
125- if (! $ instance ) {
126- throw new \InvalidArgumentException ("Workflow not found: {$ workflowId }" );
127- }
128-
129- return $ instance ;
122+ return $ this ->stateManager ->load ($ workflowId );
130123 }
131124
132125 /**
@@ -171,4 +164,14 @@ public function listWorkflows(array $filters = []): array
171164 ];
172165 }, $ instances );
173166 }
167+
168+ /**
169+ * Safely dispatch an event if event dispatcher is available
170+ */
171+ private function dispatchEvent (object $ event ): void
172+ {
173+ if ($ this ->eventDispatcher !== null ) {
174+ $ this ->eventDispatcher ->dispatch ($ event );
175+ }
176+ }
174177}
0 commit comments