55use Exception ;
66use Illuminate \Database \Eloquent \Builder ;
77use Illuminate \Support \Carbon ;
8+ use Illuminate \Support \Collection ;
89use Illuminate \Support \Facades \DB ;
10+ use Stackkit \LaravelGoogleCloudTasksQueue \Entities \StatRow ;
911use const STR_PAD_LEFT ;
1012
1113class CloudTasksApiController
1214{
13- public function dashboard ()
15+ public function dashboard (): array
1416 {
1517 $ dbDriver = config ('database.connections. ' . config ('database.default ' ) . '.driver ' );
1618
@@ -29,6 +31,9 @@ public function dashboard()
2931 ],
3032 ][$ dbDriver ];
3133
34+ /**
35+ * @var array<StatRow> $stats
36+ */
3237 $ stats = DB ::table ((new StackkitCloudTask ())->getTable ())
3338 ->where ('created_at ' , '>= ' , now ()->utc ()->startOfDay ())
3439 ->select (
@@ -52,6 +57,7 @@ public function dashboard()
5257 ]
5358 )
5459 ->get ()
60+ ->map (fn ($ row ) => StatRow::createFromObject ($ row ))
5561 ->toArray ();
5662
5763 $ response = [
@@ -98,6 +104,9 @@ public function dashboard()
98104 return $ response ;
99105 }
100106
107+ /**
108+ * @return Collection<int, StackkitCloudTask>
109+ */
101110 public function tasks ()
102111 {
103112 Carbon::setTestNowAndTimezone (now ()->utc ());
@@ -112,12 +121,12 @@ public function tasks()
112121 [$ hour , $ minute ] = explode (': ' , request ('time ' ));
113122
114123 return $ builder
115- ->where ('created_at ' , '>= ' , now ()->setTime ($ hour , $ minute , 0 ))
116- ->where ('created_at ' , '<= ' , now ()->setTime ($ hour , $ minute , 59 ));
124+ ->where ('created_at ' , '>= ' , now ()->setTime (( int ) $ hour , ( int ) $ minute , 0 ))
125+ ->where ('created_at ' , '<= ' , now ()->setTime (( int ) $ hour , ( int ) $ minute , 59 ));
117126 })
118127 ->when (request ('hour ' ), function (Builder $ builder , $ hour ) {
119- return $ builder ->where ('created_at ' , '>= ' , now ()->setTime ($ hour , 0 , 0 ))
120- ->where ('created_at ' , '<= ' , now ()->setTime ($ hour , 59 , 59 ));
128+ return $ builder ->where ('created_at ' , '>= ' , now ()->setTime (( int ) $ hour , 0 , 0 ))
129+ ->where ('created_at ' , '<= ' , now ()->setTime (( int ) $ hour , 59 , 59 ));
121130 })
122131 ->when (request ('queue ' ), function (Builder $ builder , $ queue ) {
123132 return $ builder ->where ('queue ' , $ queue );
@@ -130,26 +139,23 @@ public function tasks()
130139
131140 $ maxId = $ tasks ->max ('id ' );
132141
133- return $ tasks ->map (function (StackkitCloudTask $ task ) use ($ tasks , $ maxId )
142+ return $ tasks ->map (function (StackkitCloudTask $ task ) use ($ maxId )
134143 {
135- return [
136- 'uuid ' => $ task ->task_uuid ,
137- 'id ' => str_pad ($ task ->id , strlen ($ maxId ), 0 , STR_PAD_LEFT ),
138- 'name ' => $ task ->name ,
139- 'status ' => $ task ->status ,
140- 'attempts ' => $ task ->getNumberOfAttempts (),
141- 'created ' => $ task ->created_at -> diffForHumans (),
142- 'queue ' => $ task ->queue ,
143- ];
144- });
144+ return [
145+ 'uuid ' => $ task ->task_uuid ,
146+ 'id ' => str_pad (( string ) $ task ->id , strlen ($ maxId ), ' 0 ' , STR_PAD_LEFT ),
147+ 'name ' => $ task ->name ,
148+ 'status ' => $ task ->status ,
149+ 'attempts ' => $ task ->getNumberOfAttempts (),
150+ 'created ' => $ task ->created_at ? $ task -> created_at -> diffForHumans () : null ,
151+ 'queue ' => $ task ->queue ,
152+ ];
153+ });
145154 }
146155
147- public function task ($ uuid )
156+ public function task (string $ uuid ): array
148157 {
149- /**
150- * @var StackkitCloudTask $task
151- */
152- $ task = StackkitCloudTask::whereTaskUuid ($ uuid )->firstOrFail ();
158+ $ task = StackkitCloudTask::findByUuid ($ uuid );
153159
154160 return [
155161 'id ' => $ task ->id ,
@@ -160,4 +166,4 @@ public function task($uuid)
160166 'exception ' => $ task ->getMetadata ()['exception ' ] ?? null ,
161167 ];
162168 }
163- }
169+ }
0 commit comments