@@ -111,41 +111,16 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
111111
112112 $ payload = (array ) json_decode ($ payload , true );
113113
114- $ task = new Task ([
115- 'name ' => $ this ->taskName ($ queue , $ payload ),
116- ]);
114+ $ task = tap (new Task ())->setName ($ this ->taskName ($ queue , $ payload ));
117115
118- $ payload = $ this ->withAttempts ($ payload );
119- $ payload = $ this ->withQueueName ($ payload , $ queue );
120- $ payload = $ this ->withTaskName ($ payload , $ task ->getName ());
121- $ payload = $ this ->withConnectionName ($ payload , $ this ->getConnectionName ());
122- $ payload = $ this ->withSecurityKey ($ payload );
123-
124- if (! empty ($ this ->config ['app_engine ' ])) {
125- $ path = \Safe \parse_url (route ('cloud-tasks.handle-task ' ), PHP_URL_PATH );
126-
127- $ appEngineRequest = new AppEngineHttpRequest ();
128- $ appEngineRequest ->setRelativeUri ($ path );
129- $ appEngineRequest ->setHttpMethod (HttpMethod::POST );
130- $ appEngineRequest ->setBody (json_encode ($ payload ));
131- if (! empty ($ service = $ this ->config ['app_engine_service ' ])) {
132- $ routing = new AppEngineRouting ();
133- $ routing ->setService ($ service );
134- $ appEngineRequest ->setAppEngineRouting ($ routing );
135- }
136- $ task ->setAppEngineHttpRequest ($ appEngineRequest );
137- } else {
138- $ httpRequest = new HttpRequest ();
139- $ httpRequest ->setUrl ($ this ->getHandler ());
140- $ httpRequest ->setHttpMethod (HttpMethod::POST );
141-
142- $ httpRequest ->setBody (json_encode ($ payload ));
116+ $ payload = $ this ->enrichPayloadWithInternalData (
117+ payload: $ payload ,
118+ taskName: $ task ->getName (),
119+ connectionName: $ this ->getConnectionName (),
120+ queueName: $ queue ,
121+ );
143122
144- $ token = new OidcToken ;
145- $ token ->setServiceAccountEmail ($ this ->config ['service_account_email ' ]);
146- $ httpRequest ->setOidcToken ($ token );
147- $ task ->setHttpRequest ($ httpRequest );
148- }
123+ $ this ->addPayloadToTask ($ payload , $ task );
149124
150125 // The deadline for requests sent to the app. If the app does not respond by
151126 // this deadline then the request is cancelled and the attempt is marked as
@@ -190,52 +165,60 @@ private function sanitizeTaskName(string $taskName): string
190165 return trim ($ sanitizedName , '- ' );
191166 }
192167
193- private function withAttempts (array $ payload ): array
168+ private function enrichPayloadWithInternalData (
169+ array $ payload ,
170+ string $ queueName ,
171+ string $ taskName ,
172+ string $ connectionName ,
173+ ): array
194174 {
195- if (! isset ($ payload ['internal ' ]['attempts ' ])) {
196- $ payload ['internal ' ]['attempts ' ] = 0 ;
197- }
175+ $ payload ['internal ' ] = [
176+ 'attempts ' => $ payload ['internal ' ]['attempts ' ] ?? 0 ,
177+ 'queue ' => $ queueName ,
178+ 'taskName ' => $ taskName ,
179+ 'connection ' => $ connectionName ,
180+ ];
198181
199182 return $ payload ;
200183 }
201184
202- private function withQueueName (array $ payload , string $ queueName ): array
185+ public function addPayloadToTask (array $ payload , Task $ task ): Task
203186 {
204- $ payload ['internal ' ]['queue ' ] = $ queueName ;
205-
206- return $ payload ;
207- }
187+ if (!empty ($ this ->config ['app_engine ' ])) {
188+ $ path = \Safe \parse_url (route ('cloud-tasks.handle-task ' ), PHP_URL_PATH );
208189
209- private function withTaskName (array $ payload , string $ taskName ): array
210- {
211- $ payload ['internal ' ]['taskName ' ] = $ taskName ;
190+ $ appEngineRequest = new AppEngineHttpRequest ();
191+ $ appEngineRequest ->setRelativeUri ($ path );
192+ $ appEngineRequest ->setHttpMethod (HttpMethod::POST );
193+ $ appEngineRequest ->setBody (json_encode ($ payload ));
212194
213- return $ payload ;
214- }
195+ if (!empty ($ service = $ this ->config ['app_engine_service ' ])) {
196+ $ routing = new AppEngineRouting ();
197+ $ routing ->setService ($ service );
198+ $ appEngineRequest ->setAppEngineRouting ($ routing );
199+ }
215200
216- private function withConnectionName (array $ payload , string $ connectionName ): array
217- {
218- $ payload ['internal ' ]['connection ' ] = $ connectionName ;
201+ $ task ->setAppEngineHttpRequest ($ appEngineRequest );
202+ } else {
203+ $ httpRequest = new HttpRequest ();
204+ $ httpRequest ->setUrl ($ this ->getHandler ());
219205
220- return $ payload ;
221- }
206+ $ httpRequest -> setBody ( json_encode ( $ payload)) ;
207+ $ httpRequest -> setHttpMethod (HttpMethod:: POST );
222208
223- private function withSecurityKey (array $ payload ): array
224- {
225- $ payload ['internal ' ]['securityKey ' ] = encrypt ($ this ->config ['security_key ' ] ?? $ payload ['uuid ' ]);
209+ $ token = new OidcToken ;
210+ $ token ->setServiceAccountEmail ($ this ->config ['service_account_email ' ]);
211+ $ httpRequest ->setOidcToken ($ token );
212+ $ task ->setHttpRequest ($ httpRequest );
213+ }
226214
227- return $ payload ;
215+ return $ task ;
228216 }
229217
230- /**
231- * Pop the next job off of the queue.
232- *
233- * @param string|null $queue
234- * @return \Illuminate\Contracts\Queue\Job|null
235- */
236218 public function pop ($ queue = null )
237219 {
238- //
220+ // It is not possible to pop a job from the queue.
221+ return null ;
239222 }
240223
241224 public function delete (CloudTasksJob $ job ): void
0 commit comments