Skip to content

Commit b8e5215

Browse files
author
palPalani
committed
Improved Code
1 parent 3090313 commit b8e5215

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Sqs/Connector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace palPalani\SqsQueueReader\Sqs;
46

57
use Aws\Sqs\SqsClient;

src/Sqs/Queue.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace palPalani\SqsQueueReader\Sqs;
46

57
use Aws\Exception\AwsException;
@@ -18,23 +20,24 @@ class Queue extends SqsQueue
1820
/**
1921
* Create a payload string from the given job and data.
2022
*
21-
* @param object|string $job
22-
* @param mixed $data
23-
* @param string $queue
23+
* @param object|string $job
24+
* @param mixed $data
25+
* @param string $queue
2426
* @return string
27+
* @throws \JsonException
2528
*/
26-
protected function createPayload($job, $queue = null, $data = '')
29+
protected function createPayload($job, $queue = null, $data = ''): string
2730
{
2831
if (! $job instanceof DispatcherJob) {
2932
return parent::createPayload($job, $queue, $data);
3033
}
3134

3235
$handlerJob = $this->getClass($queue) . '@handle';
3336

34-
return $job->isPlain() ? json_encode($job->getPayload()) : json_encode([
37+
return $job->isPlain() ? \json_encode($job->getPayload(), JSON_THROW_ON_ERROR) : \json_encode([
3538
'job' => $handlerJob,
3639
'data' => $job->getPayload(),
37-
]);
40+
], JSON_THROW_ON_ERROR);
3841
}
3942

4043
/**
@@ -104,22 +107,23 @@ public function pop($queue = null)
104107
* @param string|array $payload
105108
* @param string $class
106109
* @return array
110+
* @throws \JsonException
107111
*/
108112
private function modifySinglePayload($payload, $class)
109113
{
110114
if (! is_array($payload)) {
111-
$payload = json_decode($payload, true);
115+
$payload = \json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
112116
}
113117

114-
$body = json_decode($payload['Body'], true);
118+
$body = \json_decode($payload['Body'], true, 512, JSON_THROW_ON_ERROR);
115119

116120
$body = [
117121
'uuid' => (string) Str::uuid(),
118122
'job' => $class . '@handle',
119-
'data' => isset($body['data']) ? $body['data'] : $body,
123+
'data' => $body['data'] ?? $body,
120124
];
121125

122-
$payload['Body'] = json_encode($body);
126+
$payload['Body'] = \json_encode($body, JSON_THROW_ON_ERROR);
123127

124128
return $payload;
125129
}
@@ -128,19 +132,20 @@ private function modifySinglePayload($payload, $class)
128132
* @param string|array $payload
129133
* @param string $class
130134
* @return array
135+
* @throws \JsonException
131136
*/
132137
private function modifyMultiplePayload($payload, $class)
133138
{
134139
if (! is_array($payload)) {
135-
$payload = json_decode($payload, true);
140+
$payload = \json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
136141
}
137142

138143
$body = [];
139144
$attributes = [];
140145

141146
foreach ($payload as $k => $item) {
142147
$body[$k] = [
143-
'messages' => json_decode($item['Body'], true),
148+
'messages' => \json_decode($item['Body'], true, 512, JSON_THROW_ON_ERROR),
144149
'attributes' => $item['Attributes'],
145150
'batchIds' => [
146151
'Id' => $item['MessageId'],
@@ -161,7 +166,7 @@ private function modifyMultiplePayload($payload, $class)
161166
return [
162167
'MessageId' => $messageId,
163168
'ReceiptHandle' => $receiptHandle,
164-
'Body' => json_encode($body),
169+
'Body' => \json_encode($body, JSON_THROW_ON_ERROR),
165170
'Attributes' => $attributes,
166171
];
167172
}
@@ -171,15 +176,16 @@ private function modifyMultiplePayload($payload, $class)
171176
* @param null|string $queue
172177
* @param array $options
173178
* @return mixed|null
179+
* @throws \JsonException
174180
*/
175181
public function pushRaw($payload, $queue = null, array $options = [])
176182
{
177-
$payload = json_decode($payload, true);
183+
$payload = \json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
178184

179185
if (isset($payload['data']) && isset($payload['job'])) {
180186
$payload = $payload['data'];
181187
}
182188

183-
return parent::pushRaw(json_encode($payload), $queue, $options);
189+
return parent::pushRaw(\json_encode($payload, JSON_THROW_ON_ERROR), $queue, $options);
184190
}
185191
}

0 commit comments

Comments
 (0)