@@ -12,6 +12,7 @@ class TaskHandler
12
12
{
13
13
private $ request ;
14
14
private $ publicKey ;
15
+ private $ config ;
15
16
16
17
public function __construct (CloudTasksClient $ client , Request $ request , OpenIdVerificator $ publicKey )
17
18
{
@@ -28,20 +29,29 @@ public function handle($task = null)
28
29
{
29
30
$ task = $ task ?: $ this ->captureTask ();
30
31
31
- $ command = unserialize ($ task ['data ' ]['command ' ]);
32
- $ connection = $ command ->connection ?? config ('queue.default ' );
32
+ $ this ->loadQueueConnectionConfiguration ($ task );
33
33
34
- $ this ->authorizeRequest ($ connection );
34
+ $ this ->authorizeRequest ();
35
35
36
- $ this ->listenForEvents ($ connection );
36
+ $ this ->listenForEvents ();
37
+
38
+ $ this ->handleTask ($ task );
39
+ }
37
40
38
- $ this ->handleTask ($ connection , $ task );
41
+ private function loadQueueConnectionConfiguration ($ task )
42
+ {
43
+ $ command = unserialize ($ task ['data ' ]['command ' ]);
44
+ $ connection = $ command ->connection ?? config ('queue.default ' );
45
+ $ this ->config = array_merge (
46
+ config ("queue.connections. {$ connection }" ),
47
+ ['connection ' => $ connection ]
48
+ );
39
49
}
40
50
41
51
/**
42
52
* @throws CloudTasksException
43
53
*/
44
- public function authorizeRequest ($ connection )
54
+ public function authorizeRequest ()
45
55
{
46
56
if (!$ this ->request ->hasHeader ('Authorization ' )) {
47
57
throw new CloudTasksException ('Missing [Authorization] header ' );
@@ -52,7 +62,7 @@ public function authorizeRequest($connection)
52
62
53
63
$ decodedToken = $ this ->publicKey ->decodeOpenIdToken ($ openIdToken , $ kid );
54
64
55
- $ this ->validateToken ($ connection , $ decodedToken );
65
+ $ this ->validateToken ($ decodedToken );
56
66
}
57
67
58
68
/**
@@ -61,13 +71,13 @@ public function authorizeRequest($connection)
61
71
* @param $openIdToken
62
72
* @throws CloudTasksException
63
73
*/
64
- protected function validateToken ($ connection , $ openIdToken )
74
+ protected function validateToken ($ openIdToken )
65
75
{
66
76
if (!in_array ($ openIdToken ->iss , ['https://accounts.google.com ' , 'accounts.google.com ' ])) {
67
77
throw new CloudTasksException ('The given OpenID token is not valid ' );
68
78
}
69
79
70
- if ($ openIdToken ->aud != Config:: handler ( $ connection ) ) {
80
+ if ($ openIdToken ->aud != $ this -> config [ ' handler ' ] ) {
71
81
throw new CloudTasksException ('The given OpenID token is not valid ' );
72
82
}
73
83
@@ -96,11 +106,11 @@ private function captureTask()
96
106
return $ task ;
97
107
}
98
108
99
- private function listenForEvents ($ connection )
109
+ private function listenForEvents ()
100
110
{
101
- app ('events ' )->listen (JobFailed::class, function ($ event ) use ( $ connection ) {
111
+ app ('events ' )->listen (JobFailed::class, function ($ event ) {
102
112
app ('queue.failer ' )->log (
103
- $ connection , $ event ->job ->getQueue (),
113
+ $ this -> config [ ' connection ' ] , $ event ->job ->getQueue (),
104
114
$ event ->job ->getRawBody (), $ event ->exception
105
115
);
106
116
});
@@ -110,24 +120,24 @@ private function listenForEvents($connection)
110
120
* @param $task
111
121
* @throws CloudTasksException
112
122
*/
113
- private function handleTask ($ connection , $ task )
123
+ private function handleTask ($ task )
114
124
{
115
125
$ job = new CloudTasksJob ($ task );
116
126
117
127
$ job ->setAttempts (request ()->header ('X-CloudTasks-TaskRetryCount ' ) + 1 );
118
128
$ job ->setQueue (request ()->header ('X-Cloudtasks-Queuename ' ));
119
- $ job ->setMaxTries ($ this ->getQueueMaxTries ($ connection , $ job ));
129
+ $ job ->setMaxTries ($ this ->getQueueMaxTries ($ job ));
120
130
121
131
$ worker = $ this ->getQueueWorker ();
122
132
123
- $ worker ->process ($ connection , $ job , new WorkerOptions ());
133
+ $ worker ->process ($ this -> config [ ' connection ' ] , $ job , new WorkerOptions ());
124
134
}
125
135
126
- private function getQueueMaxTries ($ connection , CloudTasksJob $ job )
136
+ private function getQueueMaxTries (CloudTasksJob $ job )
127
137
{
128
138
$ queueName = $ this ->client ->queueName (
129
- Config:: project ( $ connection ) ,
130
- Config:: location ( $ connection ) ,
139
+ $ this -> config [ ' project ' ] ,
140
+ $ this -> config [ ' location ' ] ,
131
141
$ job ->getQueue ()
132
142
);
133
143
0 commit comments