55namespace Workflow \Models ;
66
77use Illuminate \Database \Eloquent \Builder ;
8+ use Illuminate \Database \Eloquent \Collection ;
89use Illuminate \Database \Eloquent \Model ;
910use Illuminate \Database \Eloquent \Prunable ;
1011use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
12+ use Illuminate \Support \Arr ;
1113use Spatie \ModelStates \HasStates ;
1214use Workflow \States \WorkflowContinuedStatus ;
1315use Workflow \States \WorkflowStatus ;
16+ use Workflow \WorkflowMetadata ;
17+ use Workflow \WorkflowOptions ;
1418use Workflow \WorkflowStub ;
1519
1620class StoredWorkflow extends Model
@@ -52,12 +56,130 @@ public function toWorkflow()
5256 return WorkflowStub::fromStoredWorkflow ($ this );
5357 }
5458
59+ public function workflowMetadata (): WorkflowMetadata
60+ {
61+ $ arguments = $ this ->arguments ;
62+
63+ if ($ arguments === null ) {
64+ return new WorkflowMetadata ([]);
65+ }
66+
67+ return WorkflowMetadata::fromSerializedArguments (
68+ \Workflow \Serializers \Serializer::unserialize ($ arguments )
69+ );
70+ }
71+
72+ /**
73+ * @return array<int, mixed>
74+ */
75+ public function workflowArguments (): array
76+ {
77+ return $ this ->workflowMetadata ()
78+ ->arguments ;
79+ }
80+
81+ public function workflowOptions (): WorkflowOptions
82+ {
83+ return $ this ->workflowMetadata ()
84+ ->options ;
85+ }
86+
87+ public function effectiveConnection (): ?string
88+ {
89+ $ connection = $ this ->workflowOptions ()
90+ ->connection ;
91+
92+ if ($ connection !== null ) {
93+ return $ connection ;
94+ }
95+
96+ if (! is_string ($ this ->class ) || $ this ->class === '' ) {
97+ return null ;
98+ }
99+
100+ return Arr::get (WorkflowStub::getDefaultProperties ($ this ->class ), 'connection ' );
101+ }
102+
103+ public function effectiveQueue (): ?string
104+ {
105+ $ queue = $ this ->workflowOptions ()
106+ ->queue ;
107+
108+ if ($ queue !== null ) {
109+ return $ queue ;
110+ }
111+
112+ if (! is_string ($ this ->class ) || $ this ->class === '' ) {
113+ return null ;
114+ }
115+
116+ $ connection = $ this ->effectiveConnection () ?? config ('queue.default ' );
117+
118+ return Arr::get (WorkflowStub::getDefaultProperties ($ this ->class ), 'queue ' )
119+ ?? config ('queue.connections. ' . $ connection . '.queue ' , 'default ' );
120+ }
121+
55122 public function logs (): \Illuminate \Database \Eloquent \Relations \HasMany
56123 {
57124 return $ this ->hasMany (config ('workflows.stored_workflow_log_model ' , StoredWorkflowLog::class))
58125 ->orderBy ('id ' );
59126 }
60127
128+ public function findLogByIndex (int $ index , bool $ fresh = false ): ?StoredWorkflowLog
129+ {
130+ if ($ fresh ) {
131+ $ log = $ this ->logs ()
132+ ->whereIndex ($ index )
133+ ->first ();
134+
135+ if ($ this ->relationLoaded ('logs ' ) && $ log !== null ) {
136+ /** @var Collection<int, StoredWorkflowLog> $logs */
137+ $ logs = $ this ->getRelation ('logs ' );
138+ if (! $ logs ->contains ('id ' , $ log ->id )) {
139+ $ this ->setRelation ('logs ' , $ logs ->push ($ log )->sortBy ('id ' )->values ());
140+ }
141+ }
142+
143+ return $ log ;
144+ }
145+
146+ if ($ this ->relationLoaded ('logs ' )) {
147+ /** @var Collection<int, StoredWorkflowLog> $logs */
148+ $ logs = $ this ->getRelation ('logs ' );
149+ return $ logs ->firstWhere ('index ' , $ index );
150+ }
151+
152+ return $ this ->logs ()
153+ ->whereIndex ($ index )
154+ ->first ();
155+ }
156+
157+ public function hasLogByIndex (int $ index ): bool
158+ {
159+ if ($ this ->relationLoaded ('logs ' )) {
160+ return $ this ->findLogByIndex ($ index ) !== null ;
161+ }
162+
163+ return $ this ->logs ()
164+ ->whereIndex ($ index )
165+ ->exists ();
166+ }
167+
168+ public function createLog (array $ attributes ): StoredWorkflowLog
169+ {
170+ /** @var StoredWorkflowLog $log */
171+ $ log = $ this ->logs ()
172+ ->create ($ attributes );
173+
174+ if ($ this ->relationLoaded ('logs ' )) {
175+ /** @var Collection<int, StoredWorkflowLog> $logs */
176+ $ logs = $ this ->getRelation ('logs ' );
177+ $ this ->setRelation ('logs ' , $ logs ->push ($ log )->sortBy ('id ' )->values ());
178+ }
179+
180+ return $ log ;
181+ }
182+
61183 public function signals (): \Illuminate \Database \Eloquent \Relations \HasMany
62184 {
63185 return $ this ->hasMany (config ('workflows.stored_workflow_signal_model ' , StoredWorkflowSignal::class))
@@ -70,6 +192,48 @@ public function timers(): \Illuminate\Database\Eloquent\Relations\HasMany
70192 ->orderBy ('id ' );
71193 }
72194
195+ public function findTimerByIndex (int $ index ): ?StoredWorkflowTimer
196+ {
197+ if ($ this ->relationLoaded ('timers ' )) {
198+ /** @var Collection<int, StoredWorkflowTimer> $timers */
199+ $ timers = $ this ->getRelation ('timers ' );
200+ return $ timers ->firstWhere ('index ' , $ index );
201+ }
202+
203+ return $ this ->timers ()
204+ ->whereIndex ($ index )
205+ ->first ();
206+ }
207+
208+ public function createTimer (array $ attributes ): StoredWorkflowTimer
209+ {
210+ /** @var StoredWorkflowTimer $timer */
211+ $ timer = $ this ->timers ()
212+ ->create ($ attributes );
213+
214+ if ($ this ->relationLoaded ('timers ' )) {
215+ /** @var Collection<int, StoredWorkflowTimer> $timers */
216+ $ timers = $ this ->getRelation ('timers ' );
217+ $ this ->setRelation ('timers ' , $ timers ->push ($ timer )->sortBy ('id ' )->values ());
218+ }
219+
220+ return $ timer ;
221+ }
222+
223+ public function orderedSignals (): Collection
224+ {
225+ if ($ this ->relationLoaded ('signals ' )) {
226+ /** @var Collection<int, StoredWorkflowSignal> $signals */
227+ $ signals = $ this ->getRelation ('signals ' );
228+ return $ signals ->sortBy ('created_at ' )
229+ ->values ();
230+ }
231+
232+ return $ this ->signals ()
233+ ->orderBy ('created_at ' )
234+ ->get ();
235+ }
236+
73237 public function exceptions (): \Illuminate \Database \Eloquent \Relations \HasMany
74238 {
75239 return $ this ->hasMany (config ('workflows.stored_workflow_exception_model ' , StoredWorkflowException::class))
0 commit comments