Skip to content

Commit 0728bb5

Browse files
committed
Refactor
1 parent a99a0e9 commit 0728bb5

File tree

9 files changed

+66
-113
lines changed

9 files changed

+66
-113
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
}
99
],
1010
"require": {
11+
"php": "^8.1",
1112
"ext-json": "*",
1213
"phpseclib/phpseclib": "^3.0",
1314
"google/auth": "^v1.29.1",

src/CloudTasksApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
class CloudTasksApi extends Facade
1616
{
17-
protected static function getFacadeAccessor()
17+
protected static function getFacadeAccessor(): string
1818
{
1919
return 'cloud-tasks-api';
2020
}

src/CloudTasksApiConcrete.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@
1212

1313
class CloudTasksApiConcrete implements CloudTasksApiContract
1414
{
15-
/**
16-
* @var CloudTasksClient
17-
*/
18-
private $client;
19-
20-
public function __construct(CloudTasksClient $client)
15+
public function __construct(private readonly CloudTasksClient $client)
2116
{
22-
$this->client = $client;
17+
//
2318
}
2419

2520
public function createTask(string $queueName, Task $task): Task

src/CloudTasksConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class CloudTasksConnector implements ConnectorInterface
1111
{
12-
public function connect(array $config)
12+
public function connect(array $config): CloudTasksQueue
1313
{
1414
// The handler is the URL which Cloud Tasks will call with the job payload. This
1515
// URL of the handler can be manually set through an environment variable, but

src/CloudTasksJob.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@
1313

1414
class CloudTasksJob extends LaravelJob implements JobContract
1515
{
16-
/**
17-
* The Cloud Tasks raw job payload (request payload).
18-
*/
16+
protected $container;
17+
18+
private CloudTasksQueue $cloudTasksQueue;
19+
1920
public array $job;
2021

21-
/**
22-
* @var CloudTasksQueue
23-
*/
24-
public $cloudTasksQueue;
22+
protected $connectionName;
2523

26-
public function __construct(array $job, CloudTasksQueue $cloudTasksQueue)
24+
protected $queue;
25+
26+
public function __construct(Container $container, CloudTasksQueue $cloudTasksQueue, $job, $connectionName, $queue)
2727
{
28-
$this->job = $job;
29-
$this->container = Container::getInstance();
28+
$this->container = $container;
3029
$this->cloudTasksQueue = $cloudTasksQueue;
31-
32-
$command = TaskHandler::getCommandProperties($job['data']['command']);
33-
$this->queue = $command['queue'] ?? config('queue.connections.'.config('queue.default').'.queue');
30+
$this->job = $job;
31+
$this->connectionName = $connectionName;
32+
$this->queue = $queue;
3433
}
3534

36-
public function job()
35+
public function job(): array
3736
{
3837
return $this->job;
3938
}
@@ -63,9 +62,9 @@ public function setAttempts(int $attempts): void
6362
$this->job['internal']['attempts'] = $attempts;
6463
}
6564

66-
public function setQueue(string $queue): void
65+
public function getTaskName(): string
6766
{
68-
$this->queue = $queue;
67+
return $this->job['internal']['taskName'];
6968
}
7069

7170
public function delete(): void
@@ -95,9 +94,9 @@ public function hasError(): bool
9594
return data_get($this->job, 'internal.errored') === true;
9695
}
9796

98-
public function release($delay = 0)
97+
public function release($delay = 0): void
9998
{
100-
parent::release();
99+
parent::release($delay);
101100

102101
$this->cloudTasksQueue->release($this, $delay);
103102

src/CloudTasksQueue.php

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,21 @@
2020

2121
use function Safe\json_decode;
2222
use function Safe\json_encode;
23+
use function Safe\preg_replace;
2324

2425
class CloudTasksQueue extends LaravelQueue implements QueueContract
2526
{
26-
/**
27-
* @var CloudTasksClient
28-
*/
29-
private $client;
30-
31-
public array $config;
32-
33-
public function __construct(array $config, CloudTasksClient $client, $dispatchAfterCommit = false)
27+
public function __construct(public array $config, public CloudTasksClient $client, public $dispatchAfterCommit = false)
3428
{
35-
$this->client = $client;
36-
$this->config = $config;
37-
$this->dispatchAfterCommit = $dispatchAfterCommit;
29+
//
3830
}
3931

4032
/**
4133
* Get the size of the queue.
4234
*
4335
* @param string|null $queue
44-
* @return int
4536
*/
46-
public function size($queue = null)
37+
public function size($queue = null): int
4738
{
4839
// It is not possible to know the number of tasks in the queue.
4940
return 0;
@@ -61,7 +52,7 @@ public function push($job, $data = '', $queue = null)
6152
{
6253
return $this->enqueueUsing(
6354
$job,
64-
$this->createPayload($job, $this->getQueue($queue), $data),
55+
$this->createPayload($job, $queue, $data),
6556
$queue,
6657
null,
6758
function ($payload, $queue) {
@@ -97,7 +88,7 @@ public function later($delay, $job, $data = '', $queue = null)
9788
{
9889
return $this->enqueueUsing(
9990
$job,
100-
$this->createPayload($job, $this->getQueue($queue), $data),
91+
$this->createPayload($job, $queue, $data),
10192
$queue,
10293
$delay,
10394
function ($payload, $queue, $delay) {
@@ -116,7 +107,7 @@ function ($payload, $queue, $delay) {
116107
*/
117108
protected function pushToCloudTasks($queue, $payload, $delay = 0)
118109
{
119-
$queue = $this->getQueue($queue);
110+
$queue = $queue ?: $this->config['queue'];
120111
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
121112
$availableAt = $this->availableAt($delay);
122113

@@ -127,9 +118,13 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
127118
// value and need to manually set and update the number of times a task has been attempted.
128119
$payload = $this->withAttempts($payload);
129120

130-
$task = $this->createTask();
121+
$payload = $this->withQueueName($payload, $queue);
122+
123+
$task = new Task();
131124
$task->setName($this->taskName($queue, $payload));
132125

126+
$payload = $this->withTaskName($payload, $task->getName());
127+
133128
if (! empty($this->config['app_engine'])) {
134129
$path = \Safe\parse_url(route('cloud-tasks.handle-task'), PHP_URL_PATH);
135130

@@ -144,7 +139,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
144139
}
145140
$task->setAppEngineHttpRequest($appEngineRequest);
146141
} else {
147-
$httpRequest = $this->createHttpRequest();
142+
$httpRequest = new HttpRequest();
148143
$httpRequest->setUrl($this->getHandler());
149144
$httpRequest->setHttpMethod(HttpMethod::POST);
150145

@@ -189,7 +184,7 @@ private function taskName(string $queueName, array $payload): string
189184
);
190185
}
191186

192-
private function sanitizeTaskName(string $taskName)
187+
private function sanitizeTaskName(string $taskName): string
193188
{
194189
// Remove all characters that are not -, letters, numbers, or whitespace
195190
$sanitizedName = preg_replace('![^-\pL\pN\s]+!u', '-', $taskName);
@@ -209,6 +204,20 @@ private function withAttempts(array $payload): array
209204
return $payload;
210205
}
211206

207+
private function withQueueName(array $payload, string $queueName): array
208+
{
209+
$payload['internal']['queue'] = $queueName;
210+
211+
return $payload;
212+
}
213+
214+
private function withTaskName(array $payload, string $taskName): array
215+
{
216+
$payload['internal']['taskName'] = $taskName;
217+
218+
return $payload;
219+
}
220+
212221
/**
213222
* Pop the next job off of the queue.
214223
*
@@ -217,35 +226,12 @@ private function withAttempts(array $payload): array
217226
*/
218227
public function pop($queue = null)
219228
{
220-
// TODO: Implement pop() method.
221-
}
222-
223-
private function getQueue(?string $queue = null): string
224-
{
225-
return $queue ?: $this->config['queue'];
226-
}
227-
228-
private function createHttpRequest(): HttpRequest
229-
{
230-
return app(HttpRequest::class);
229+
//
231230
}
232231

233232
public function delete(CloudTasksJob $job): void
234233
{
235-
$config = $this->config;
236-
237-
$queue = $job->getQueue() ?: $this->config['queue']; // @todo: make this a helper method somewhere.
238-
239-
$headerTaskName = request()->headers->get('X-Cloudtasks-Taskname')
240-
?? request()->headers->get('X-AppEngine-TaskName');
241-
$taskName = $this->client->taskName(
242-
$config['project'],
243-
$config['location'],
244-
$queue,
245-
(string) $headerTaskName
246-
);
247-
248-
CloudTasksApi::deleteTask($taskName);
234+
CloudTasksApi::deleteTask($job->getTaskName());
249235
}
250236

251237
public function release(CloudTasksJob $job, int $delay = 0): void
@@ -259,11 +245,6 @@ public function release(CloudTasksJob $job, int $delay = 0): void
259245
$this->pushRaw($payload, $job->getQueue(), $options);
260246
}
261247

262-
private function createTask(): Task
263-
{
264-
return app(Task::class);
265-
}
266-
267248
public function getHandler(): string
268249
{
269250
return Config::getHandler($this->config['handler']);

src/CloudTasksServiceProvider.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Queue\Events\JobProcessing;
1212
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
1313
use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased;
14-
use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated;
1514

1615
class CloudTasksServiceProvider extends LaravelServiceProvider
1716
{
@@ -74,11 +73,11 @@ private function registerEvents(): void
7473
return;
7574
}
7675

77-
$config = $event->job->cloudTasksQueue->config;
78-
7976
app('queue.failer')->log(
80-
$config['connection'], $event->job->getQueue() ?: $config['queue'],
81-
$event->job->getRawBody(), $event->exception
77+
$event->job->getConnectionName(),
78+
$event->job->getQueue(),
79+
$event->job->getRawBody(),
80+
$event->exception,
8281
);
8382
});
8483

src/Events/JobReleased.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,8 @@
88

99
class JobReleased
1010
{
11-
/**
12-
* The connection name.
13-
*/
14-
public string $connectionName;
15-
16-
/**
17-
* The job instance.
18-
*/
19-
public Job $job;
20-
21-
/**
22-
* The job delay in seconds.
23-
*/
24-
public int $delay;
25-
26-
/**
27-
* Create a new event instance.
28-
*
29-
* @return void
30-
*/
31-
public function __construct(string $connectionName, Job $job, int $delay = 0)
11+
public function __construct(public string $connectionName, public Job $job, public int $delay = 0)
3212
{
33-
$this->job = $job;
34-
$this->connectionName = $connectionName;
35-
$this->delay = $delay;
13+
//
3614
}
3715
}

src/TaskHandler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Google\ApiCore\ApiException;
88
use Google\Cloud\Tasks\V2\Client\CloudTasksClient;
9+
use Illuminate\Container\Container;
910
use Illuminate\Contracts\Encryption\Encrypter;
1011
use Illuminate\Queue\WorkerOptions;
1112
use Illuminate\Support\Str;
@@ -120,17 +121,16 @@ private function guard(): void
120121

121122
private function handleTask(array $task): void
122123
{
123-
$job = new CloudTasksJob($task, $this->queue);
124-
125-
$fullTaskName = $this->client->taskName(
126-
$this->config['project'],
127-
$this->config['location'],
128-
$job->getQueue() ?: $this->config['queue'],
129-
request()->header('X-CloudTasks-TaskName') ?? request()->header('X-AppEngine-TaskName'),
124+
$job = new CloudTasksJob(
125+
Container::getInstance(),
126+
$this->queue,
127+
$task,
128+
$this->config['connection'],
129+
$task['internal']['queue'],
130130
);
131131

132132
try {
133-
$apiTask = CloudTasksApi::getTask($fullTaskName);
133+
$apiTask = CloudTasksApi::getTask($task['internal']['taskName']);
134134
} catch (ApiException $e) {
135135
if (in_array($e->getStatus(), ['NOT_FOUND', 'PRECONDITION_FAILED'])) {
136136
abort(404);

0 commit comments

Comments
 (0)