Skip to content

Commit 19ac434

Browse files
committed
Refactor & cleanup
1 parent abe5dbc commit 19ac434

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/CloudTasksConnector.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@ class CloudTasksConnector implements ConnectorInterface
1111
{
1212
public function connect(array $config): CloudTasksQueue
1313
{
14-
// The handler is the URL which Cloud Tasks will call with the job payload. This
15-
// URL of the handler can be manually set through an environment variable, but
16-
// if it is not then we will choose a sensible default (the current app url)
17-
if (empty($config['handler'])) {
18-
// At this point (during service provider boot) the trusted proxy middleware
19-
// has not been set up, and so we are not ready to get the scheme and host
20-
// So we wrap it and get it later, after the middleware has been set up.
21-
$config['handler'] = function () {
22-
return request()->getSchemeAndHttpHost();
23-
};
24-
}
25-
26-
return new CloudTasksQueue($config, app(CloudTasksClient::class), $config['after_commit'] ?? null);
14+
return new CloudTasksQueue(
15+
config: $config,
16+
client: app(CloudTasksClient::class),
17+
dispatchAfterCommit: $config['after_commit'] ?? null
18+
);
2719
}
2820
}

src/CloudTasksJob.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class CloudTasksJob extends LaravelJob implements JobContract
2323

2424
protected $queue;
2525

26+
/**
27+
* @param array $job
28+
* @param string $connectionName
29+
* @param string $queue
30+
*/
2631
public function __construct(Container $container, CloudTasksQueue $cloudTasksQueue, $job, $connectionName, $queue)
2732
{
2833
$this->container = $container;

src/CloudTasksQueue.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@ function ($payload, $queue, $delay) {
108108
protected function pushToCloudTasks($queue, $payload, $delay = 0)
109109
{
110110
$queue = $queue ?: $this->config['queue'];
111-
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
112-
$availableAt = $this->availableAt($delay);
113111

114-
$payload = json_decode($payload, true);
112+
$payload = (array) json_decode($payload, true);
115113

116-
$task = new Task();
117-
$task->setName($this->taskName($queue, $payload));
114+
$task = new Task([
115+
'name' => $this->taskName($queue, $payload),
116+
]);
118117

119118
$payload = $this->withAttempts($payload);
120119
$payload = $this->withQueueName($payload, $queue);
@@ -155,10 +154,12 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
155154
$task->setDispatchDeadline(new Duration(['seconds' => $this->config['dispatch_deadline']]));
156155
}
157156

157+
$availableAt = $this->availableAt($delay);
158158
if ($availableAt > time()) {
159159
$task->setScheduleTime(new Timestamp(['seconds' => $availableAt]));
160160
}
161161

162+
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
162163
CloudTasksApi::createTask($queueName, $task);
163164

164165
event((new TaskCreated)->queue($queue)->task($task));
@@ -255,11 +256,15 @@ public function release(CloudTasksJob $job, int $delay = 0): void
255256

256257
public function getHandler(): string
257258
{
259+
if (empty($this->config['handler'])) {
260+
$this->config['handler'] = request()->getSchemeAndHttpHost();
261+
}
262+
258263
$handler = rtrim($this->config['handler'], '/');
259264

260265
return match (true) {
261266
! str_ends_with($handler, '/handle-task') => $handler.'/handle-task',
262-
default => $handler,
267+
default => $this->config['handler'],
263268
};
264269
}
265270
}

0 commit comments

Comments
 (0)